我有一个自定义的帖子类型,其中只有3个自定义字段。
$post_types = get_post_meta($post->ID,\'post_types\',true);
$post_taxonomies = get_post_meta($post->ID,\'post_taxonomies\',true);
$post_terms = get_post_meta($post->ID,\'post_terms\',true);
我想检查是否已经存在具有此$post\\U types值的帖子,如果存在->显示消息而不发布。我为这个自定义字段创建了一个元框,它已经具有save\\u post action hook的功能。
function sidebars_meta_save( $id ) {
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST[\'sidebars_nonce\'] ) || !wp_verify_nonce( $_POST[\'sidebars_nonce\'], \'save_sidebars_meta\' ) ) return;
if( !current_user_can( \'edit_post\' ) ) return;
$allowed = array(
\'p\' => array()
);
if(isset( $_POST[\'post_terms\'] )){
//check_exist_term - simple boolean function which check if
//a post with this term already exist
if(!check_exist_term($_POST[\'post_terms\'])){
if( isset( $_POST[\'post_types\'] ) ){
update_post_meta( $id, \'post_types\', wp_kses( $_POST[\'post_types\'], $allowed ) );
}
if( isset( $_POST[\'post_taxonomies\'] ) ) {
update_post_meta( $id, \'post_taxonomies\', wp_kses( $_POST[\'post_taxonomies\'], $allowed ) );
}
update_post_meta( $id, \'post_terms\', wp_kses( $_POST[\'post_terms\'], $allowed ) );
}
}else{
//if I will use wp_delete_post here, the message will not show
//wp_delete_post($id);
//wp_redirect(\'post-new.php?post_type=sidebars\');
//exit;
}
}
}
add_action( \'save_post\', \'sidebars_meta_save\' );
我的重定向位置功能:function sidebars_redirect_location($location,$post_id){
if( isset( $_POST[\'post_types\'] ) ){
if(check_exist_term($_POST[\'post_terms\'])){
$status = get_post_status( $post_id );
$location = add_query_arg(\'message\', 21, $location);
//if I will use wp_delete_post here, I will be not able
//to return to post edit page, because page will be already deleted
}
}
return $location;
}
add_filter(\'redirect_post_location\',\'sidebars_redirect_location\',10,2);
我的帖子更新消息功能:function sidebars_custom_messages($messages){
$messages[\'sidebars\'][21] = \'Looks like you tried publishing a post with term which already exist!\';
return $messages;
}
add_filter(\'post_updated_messages\', \'sidebars_custom_messages\');
1)如果我想首先显示该帖子已经存在的消息,然后删除该帖子并停留在该页面上,我应该在哪里使用wp\\u delete\\u post()?2) 如何显示红色消息而不是黄色消息?