我有一个自定义的帖子类型,其中只有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) 如何显示红色消息而不是黄色消息?
最合适的回答,由SO网友:Stephen Harris 整理而成
检查是否存在元数据,您可以使用get_posts()
(或WP_Query
对象)查询与要保存的帖子元匹配的所有帖子。您需要指定post状态(即您愿意忽略哪些状态)。以下是untested对此进行编码。
(为了完整性,我在参数中留下了一些可以省略的参数,因为它们被赋予了默认值)。我还排除了当前的帖子,以防帖子被更新,否则该帖子将被返回,我们将认为已经存在一篇包含该元数据的帖子。
//Collect the terms
//I\'m assuming that each of the values are strings.
$meta_terms = $_POST[\'post_terms\'];
$meta_taxs = $_POST[\'post_taxonomies\'];
$meta_types = $_POST[\'post_types\'];
//Query post type \'products\' to check for posts whose meta values match the POSTed ones
$args = array(
\'post__not_in\'=> array($id),
\'post_type\' => \'product\',
\'post_status\' => array(\'publish\',\'pending\',\'draft\',\'future\',\'private\'),
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'post_terms\',
\'value\' => $meta_terms,
\'compare\' => \'=\'
),
array(
\'key\' => \'post_taxonomies\',
\'value\' => $meta_taxs,
\'compare\' => \'=\'
),
array(
\'key\' => \'post_types\',
\'value\' => $meta_types
\'compare\' => \'=\'
)
)
);
$existingMeta = get_posts( $args );
if(empty($existingMeta)){
//Go ahead and save meta data
}else{
//Revert post back to draft status as shown in linked answer below.
}
“post\\u术语”等似乎相当通用。我会给它们更具描述性和唯一性的名称,如果将事件用作post元表中的键,则会更具描述性和唯一性。此外,为了避免冲突,您可能希望使用唯一的名称在数组中发送数据:即。
<input type="text" name="myplugin[post_terms]" value=""/>
然后将数据检索为
$_POST[\'myplugin\'][\'post_terms\']
而不是
$_POST[\'post_terms\']
. 您可以合理地确定
$_POST[\'myplugin\']
已被另一个插件重写。还有你
must 使用
nonces 帮助验证来源/意图。
在链接的解决方案中显示自定义消息,如果帖子已恢复为草稿,我将消息变量设置为“10”,以便显示草稿消息:“草稿已更新…”\':
add_filter(\'redirect_post_location\',\'my_redirect_location\',10,2);
function my_redirect_location($location,$post_id){
//If post was published...
if (isset($_POST[\'publish\'])){
//obtain current post status
$status = get_post_status( $post_id );
//The post was \'published\', but if it is still a draft, display draft message (10).
if($status==\'draft\')
$location = add_query_arg(\'message\', 10, $location)
}
return $location;
}
但是,您可以指定自己的消息。10表示一个数组键,其中每个值都是一条消息。0为空,1-9为默认消息(发布后、更新后等)
(这未经测试),但您可以将自己的消息添加到此数组中:
add_filter(\'post_updated_messages\', \'my_custom_messages\');
function my_custom_messages($messages){
//$message is an array of arrays.
//$message[\'post_type\'] is an array of messages for post type \'post_type\'
//It is a non-assocative array. Value for 0 is blank. Keys 1-9 hold the default messages
//Add a custom message to key 21 for post type \'my_post_type\'
$messages[\'my_post_type\'][21] = \'Looks like you tried publishing, but something went wrong\';
return $messages
}
然后,您可以将消息设置为21,而不是如上所示的10。