WordPress.org

Support

Support » How-To and Troubleshooting » set_post_status() function?

set_post_status() function?

  • I’m working on a plugin that changes a posts status (draft / published) based on its meta data.

    While there is a handy function to get a posts current status:
    get_post_status(id)
    I can’t find any record of a similar function to allow you to set the status.

    I’m currently using a custom DB call (below) but I’m worried that other crucial function calls are not happening!

    $wpdb->update(
    						$wpdb->posts,
    						array( 'post_status' => 'draft'),
    						array( 'id' => $id ),
    						array( '%s' ),
    						array( '%d' )
    				);

    Does any one know a better way to achieve this please?

    Thanks!

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try wp_update_post

    <?php
    // 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 );
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘set_post_status() function?’ is closed to new replies.
Skip to toolbar