1

I want to change the status of already publish post to draft using its id.

   add_action( 'save_post', 'change_post_status', 1 );
function change_post_status( $post_id ){
    $my_post = array(
        'ID' => 1,
        'post_status' => 'draft',
    );
    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'change_post_status');
    if( $post_id == 1 ){
        wp_update_post( $my_post );
    }
    // re-hook this function
    add_action('save_post', 'change_post_status');
}

I want to change the status of specific post which has id = 1 to be changed to draft.It is working but leading to

Maximum function nesting level of '100' reached, aborting!

why it is getting this error ?

What I think is it is running when post is updated or saving , I want to just run the function independent of save or update action like on wp_init or wp_admin or admin_init some other hook, is it possible.

  • I am not sure its right way to do but it seems your code is working for me and changed status of post to draft and if you want to do it for post having ID 1 then you can apply condition if($post_id == 1) – jas Dec 4 '15 at 10:22
  • @jas where before wp_update_post() ?? – Ghanashyam Naik Dec 4 '15 at 10:26
  • @jas Can you post it as answer ? – Ghanashyam Naik Dec 4 '15 at 10:27
  • just added in your function at start and enclosed all code in that condition. – jas Dec 4 '15 at 10:34
  • @jas that not worked for me. – Ghanashyam Naik Dec 4 '15 at 10:43
1

In your functions.php :

add_action('publish_post', 'check_user_publish', 10, 2);

function check_user_publish ($post_id, $post) {

    if($post_id == 1){
    $query = array(
        'ID' => $post_id,
        'post_status' => 'draft',
    );
    wp_update_post( $query, true );

    }

}
  • It not worked for me, how ever can you give me some idea how I should debug , so that I can show some error or something . – Ghanashyam Naik Dec 4 '15 at 10:45
  • Adding error_reporting(E_ALL); ini_set('display_errors', 1); define('WP_DEBUG', true); in your config file can show error if any is there. – jas Dec 4 '15 at 10:48
  • sorry using these also not resulting in any error. – Ghanashyam Naik Dec 4 '15 at 10:51
  • Please try updated answer I am also trying by search and reading but I think to remove hook we can use remove_filter( current_filter(), __FUNCTION__ ); like you did to avoid infinte loop – jas Dec 4 '15 at 10:57
  • 1
    where I should use it ? – Ghanashyam Naik Dec 4 '15 at 11:01
0

To remove a hook, it has to have the same priority as when you add the hook. You add_action with a priority of 1. If you want to remove the action, you need to remove it with a priority of 1. See the codex.

add_action( 'save_post', 'change_post_status', 1 );
function change_post_status( $post_id ){
  $my_post = array(
    'ID' => 1,
    'post_status' => 'draft',
  );
  // unhook this function, making sure to use the same priority, so it doesn't loop infinitely
  remove_action('save_post', 'change_post_status', 1 );
  if( $post_id == 1 ){
    wp_update_post( $my_post );
  }
  // re-hook this function with the initial priority
  add_action('save_post', 'change_post_status', 1 );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.