WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I'm running the YouTube Video Fetcher plugin at . It fetches videos using the youtube api and displays them on your website.

Within the plugin script, there is the following sequence:

    if (empty($items)) {$ret .= " 'No new videos.'";}
    else foreach ( $items as $item ) :

Is it possible to change the wordpress post status from published to draft if "No new videos" are found?

I am thinking the solution is using the wp update post function and something along the lines of the following:

        // Update post
        $my_post = array();
        $my_post['ID'] = $id;
        $my_post['post_status'] = 'draft';

        // Update the post into the database
        wp_update_post( $my_post );
share|improve this question

marked as duplicate by Pieter Goosen, Robert hue, Howdy_McGee, Mayeenul Islam, gmazzap Apr 15 '15 at 17:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

As long as your $id is set and valid this should work. Doesn't it? – tfrommen Feb 27 '14 at 7:35
up vote 4 down vote accepted

I guess it should work . As long as $id is available things are easy.

if (empty($items)) {
$ret .= " 'No new videos.'";
$postid = $post->ID; //Supply post-Id here $post->ID.
        'ID'    =>  $postid,
        'post_status'   =>  'draft'
    foreach ( $items as $item ) :

Give it a shot.

share|improve this answer
Thanks Sudeep! Worked perfectly. – Aaron Feb 28 '14 at 23:26

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