admin管理员组文章数量:1130349
I'd like to update my post status (of my own CPT) based on custom field "played". If played is 1 i want that the post shall be published, but if the custom field played is 0 the post shall be draft also if I tried to publish it.
Is it possible?
I tried to search in the forum but nothing found that works... also tried the code here but not working...
How to Update post status using meta data in Custom post TYpe
I'd like to update my post status (of my own CPT) based on custom field "played". If played is 1 i want that the post shall be published, but if the custom field played is 0 the post shall be draft also if I tried to publish it.
Is it possible?
I tried to search in the forum but nothing found that works... also tried the code here but not working...
How to Update post status using meta data in Custom post TYpe
Share Improve this question asked Nov 30, 2018 at 18:56 GiulioGiulio 511 silver badge8 bronze badges2 Answers
Reset to default 2All you need to do is to use save_post hook. Here's how:
function change_post_status_based_on_custom_field( $post_id ) {
// If this is just a revision, don't do anything.
if ( wp_is_post_revision( $post_id ) )
return;
// Get field value
$value = get_post_meta( $post_id, 'played', true );
$status = $value ? 'publish' : 'draft';
// If status should be different, change it
if ( get_post_status( $post_id ) != $status ) {
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'change_post_status_based_on_custom_field' );
// update the post, which calls save_post again
wp_update_post( array(
'ID' => $post_id,
'post_status' => $status
) );
// re-hook this function
add_action( 'save_post', 'change_post_status_based_on_custom_field' );
}
}
add_action( 'save_post', 'change_post_status_based_on_custom_field' );
That might be quite a bit of code to paste in here, but your strategy should be along the lines of:
- Hook onto post_save event:
- make a static variable that you've already checked this, to prevent infinite recursion in the case that you update the post from within this function.
- check if custom field == 'played'
- if it is, modify the post status as needed, and do wp_update_post.
I'd like to update my post status (of my own CPT) based on custom field "played". If played is 1 i want that the post shall be published, but if the custom field played is 0 the post shall be draft also if I tried to publish it.
Is it possible?
I tried to search in the forum but nothing found that works... also tried the code here but not working...
How to Update post status using meta data in Custom post TYpe
I'd like to update my post status (of my own CPT) based on custom field "played". If played is 1 i want that the post shall be published, but if the custom field played is 0 the post shall be draft also if I tried to publish it.
Is it possible?
I tried to search in the forum but nothing found that works... also tried the code here but not working...
How to Update post status using meta data in Custom post TYpe
Share Improve this question asked Nov 30, 2018 at 18:56 GiulioGiulio 511 silver badge8 bronze badges2 Answers
Reset to default 2All you need to do is to use save_post hook. Here's how:
function change_post_status_based_on_custom_field( $post_id ) {
// If this is just a revision, don't do anything.
if ( wp_is_post_revision( $post_id ) )
return;
// Get field value
$value = get_post_meta( $post_id, 'played', true );
$status = $value ? 'publish' : 'draft';
// If status should be different, change it
if ( get_post_status( $post_id ) != $status ) {
// unhook this function so it doesn't loop infinitely
remove_action( 'save_post', 'change_post_status_based_on_custom_field' );
// update the post, which calls save_post again
wp_update_post( array(
'ID' => $post_id,
'post_status' => $status
) );
// re-hook this function
add_action( 'save_post', 'change_post_status_based_on_custom_field' );
}
}
add_action( 'save_post', 'change_post_status_based_on_custom_field' );
That might be quite a bit of code to paste in here, but your strategy should be along the lines of:
- Hook onto post_save event:
- make a static variable that you've already checked this, to prevent infinite recursion in the case that you update the post from within this function.
- check if custom field == 'played'
- if it is, modify the post status as needed, and do wp_update_post.
本文标签: Change post status by custom fields
版权声明:本文标题:Change post status by custom fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749137796a2321661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论