下面是我更新post meta的代码。
function changePostExpireDatetime( $post ){
$featurePlanID = get_post_meta($post->ID, \'post_price_plan_id\', true );
remove_action(\'pending_to_publish\', \'changePostExpireDatetime\', 10, 1);
$plan_price = get_post_meta($featurePlanID, \'plan_price\', true);
update_post_meta($post->ID, \'post_plan_price\', $plan_price );
update_post_meta($post->ID, \'featured_post\', "1" );
add_action(\'pending_to_publish\', \'changePostExpireDatetime\', 10, 1);
}
add_action(\'pending_to_publish\', \'changePostExpireDatetime\', 10, 1);
调用了函数,但未更新meta。我如何在那个钩子中更新post-meta。
最合适的回答,由SO网友:Pieter Goosen 整理而成
我倾向于利用transition_post_status
钩住它,因为它让你对你需要做的事情有如此多的控制权。请确保查看所有可用状态选项的链接。我特别喜欢的是$post
对象也被传递到挂钩,因此您也可以针对特定的帖子类型。
您可以尝试以下操作:(请确保将这些值更新为您自己的值,我只是以我的值为例)
add_action( \'transition_post_status\', function ( $new_status, $old_status, $post )
{
// Check if we are transitioning from pending to publish
if ( $old_status == \'pending\' && $new_status == \'publish\' ) {
// Check whether or not the meta_key exists already with our value
if ( ! add_post_meta( $post->ID, \'post_views_count\', 50, true ) ) {
update_post_meta ( $post->ID, \'post_views_count\', 50 );
}
}
}, 10, 3 );