我需要以下功能。每当更新或保存自定义帖子类型时,我都需要覆盖某些自定义帖子元。
我需要确保这只会影响“VA\\u LISTING\\u PTYPE”类型的帖子以及具有“meta\\u key”=>“featured cat”和“meta\\u value”=>1的帖子
我目前使用的代码如下(不起作用)
//Remove urls from free listings
function remove_url_free_post( $post_id ) {
$slug = \'VA_LISTING_PTYPE\',
if ( $slug != $_POST[\'post_type\'] ) {
return;
}
$meta_values = get_post_meta( $post_id, \'featured-cat\', true );
if ( $meta_values != 1 ) {
return;
}
update_post_meta($post_id, \'website\', \'\');
update_post_meta($post_id, \'twitter\', \'\');
update_post_meta($post_id, \'facebook\', \'\');
}
add_action(\'save_post\', \'remove_url_free_post\');
我还尝试了不同的操作挂钩,如来自此的pre\\u post\\u update
answer我就是不能让它工作。目前唯一对我有效的丑陋修复方法是:
//Remove urls from free listings
function remove_url_free_post() {
//Fetches all the listings that have featured cat which equals free listing for us
$r = new WP_Query(
array(
\'post_type\' => VA_LISTING_PTYPE,
\'no_found_rows\' => true,
\'meta_key\' => \'featured-cat\',
\'meta_value\' => 1
) );
if ( $r->have_posts() ) :
while ( $r->have_posts() ) : $r->the_post();
//removes the website, twitter and facebook
$post_id3 = get_the_ID();
update_post_meta($post_id3, \'website\', \'\');
update_post_meta($post_id3, \'twitter\', \'\');
update_post_meta($post_id3, \'facebook\', \'\');
endwhile;
endif;
}
//Not ideal at all as called everytime, save_post not working as intended
add_action(\'wp_footer\', \'remove_url_free_post\');
SO网友:Ammar Alakkad
您使用“save\\u post”操作挂钩是正确的。
尝试以下操作:
<?php
add_action(\'save_post\', \'some_function\');
function some_function($post_id)
{
if(get_post_type($post_id) != "VA_LISTING_PTYPE")
return;
$meta_value = get_post_meta( $post_id, \'featured-cat\', true );
if($meta_value != 1)
return;
update_post_meta($post_id, \'website\', \'\');
update_post_meta($post_id, \'twitter\', \'\');
update_post_meta($post_id, \'facebook\', \'\');
}
如果您使用的是Wordpress 3.7或更高版本,可以这样使用:
add_action(\'save_post_VA_LISTING_PTYPE\', \'some_function\');
function some_function($post_id)
{
$meta_value = get_post_meta( $post_id, \'featured-cat\', true );
if($meta_value != 1)
return;
update_post_meta($post_id, \'website\', \'\');
update_post_meta($post_id, \'twitter\', \'\');
update_post_meta($post_id, \'facebook\', \'\');
}
我希望它能和你一起工作。