0

I have custom post type and have some some meta field it. I have a select field 'Publish' to change post status. if I set it 'yes' and click on Update button then post will be published and if i set it to 'no' then post should be save as 'Draft'. you can see screenshot of my custom post type-

enter image description here

What code should I used to in add_action( 'save_post','save_my_data'). or any other idea.?

1 Answer 1

1

If I'm understanding correctly, you want to use your own select form to update a post's published status instead of the publish/unpublish buttons? Something like this would work for that:

function save_my_data($post_id){ if($_POST['publish'] == 'yes' && get_post_status($post_id) != 'publish'){ //whatever your post variable is remove_action('save_post', 'save_my_data'); //if you don't unhook the function you'll have an infinite loop wp_publish_post($post_id); add_action('save_post', 'save_my_data'); //rehook the function } if($_POST['publish']) == 'no' && get_post_status($post_id) == 'publish'){ remove_action('save_post', 'save_my_data'); //if you don't unhook the function you'll have an infinite loop wp_update_post(array( 'ID' => $post_id, 'post_status' => 'draft' )); add_action('save_post', 'save_my_data'); //rehook the function } } 

You would probably want to add some error handling and optimize it a little bit. You also might want to account for the other post statuses outlined here: http://codex.wordpress.org/Post_Status#Default_Statuses

1
  • Simple but perfect, I had been trying for hours to find a solution but without success. Commented Jul 9, 2018 at 6:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.