我有一个自定义的属性帖子类型和一个提交它们的前端表单。wp_posts
中的post_type
为property
在wp_postmeta
表中有一个名为REAL_HOMES_property_id
的元键。当前这是一个接受用户输入的文本框。
我想做的是隐藏字段(已经完成),并填充ID
从wp_posts
,当表单提交。
是否有一个钩子,将工作在wp_postmeta
表提交后?
谢谢
将下面的代码粘贴到主题的functions.php文件中:
add_action( 'save_post', 'set_post_meta_func');
function set_post_meta_func($post_id)
{
/* return if not porpoerty post */
$post_type = get_post_type($post_id);
if ( "property" != $post_type )
return;
/* return if revision */
if(wp_is_post_revision( $post_id ))
return;
/* return if not published */
if("publish" != get_post_status($post_id))
return;
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'set_post_meta_func' );
// update the post, which calls save_post again
update_post_meta($post_id, "REAL_HOMES_property_id", "your value here");
// re-hook this function
add_action( 'save_post', 'set_post_meta_func' );
}
将"your value here"替换为你想保存的值
希望对你有所帮助!
function assign_property_id( $post_id ) {
$post_type = get_post_type($post_id);
if($post_type == 'property') {
update_post_meta( $post_id, 'REAL_HOMES_property_id', $post_id );
}
}
add_action( 'save_post', 'assign_property_id' );