WordPress.org

Ready to get started?Download WordPress

Forums

set_post_status() function? (2 posts)

  1. TwoThirdsWater
    Member
    Posted 3 years ago #

    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!

  2. keesiemeijer
    moderator
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.