4 Special Ways to Unpublish a Post Programmatically in WordPress!

4.2 from 5
from 5 user
(Rate this post)

There are 4 ways to unpublish a Post in WordPress using PHP codes. In this WordPress tutorial for beginners, we'll try unpublishing a WordPress Post using WordPress core methods.

4 Ways to Unpublish a WordPress Post Programmatically

When you develop WordPress themes or plugins, you may want to use the feature of unpublishing a WordPress post. (read my starter tutorial to Start Developing WordPress Plugins Fast!)

Using either one of these methods to unpublish a WordPress post to hide it from visitors or users.

We use the wp_update_post method to update a specific post and unpublish it.

1- Set Post Status as a Draft in WordPress using PHP

Setting post status for a post is a way to unpublish a post without any harm.

Setting post_status to draft is a preffered way of unpublishing a post.

Set Draft post status for a post (using post ID):

$postId = 1;//change to your post ID

    'ID'    =>  $postId,
    'post_status'   =>  'draft'

2- Make the Post Visibility Private in WordPress using PHP

Private posts are only accessible to admins and editors. If you set post visibility as private, it will be hidden to visitors.

Setting post_status of a post to private is a way of unpublishing WordPress posts.

Use this code to set post visibility of an ID to Private:

$postId = 1;//change to your post ID

    'ID'    =>  $postId,
    'post_status'   =>  'private'

3- Set Publish Date for Future in WordPress using PHP

Setting a publish date for posts can do the trick too! you can use a future date to unpublish WordPress posts safe and without consequences.

I wrote a blog post about setting WordPress post publishing date programmatically. you can read it for more information.

Example for setting WordPress post publish date:

$postId = 1;//change to your post ID

$date_time = "2050-12-31 23:59:59";//[Y-m-d H:i:s]
    array (
        'ID' => $postId,
        'post_date' => $date_time,
        'post_date_gmt' => get_gmt_from_date( $date_time )

4- Delete the Post in WordPress using PHP!

This is a harsh decision! removing a blog post is not something you decide this fast.

Display Warnings to the decision-makers if you are using this feature in a plugin.

$postId = 12341;

wp_delete_post($postId, false); //change false to true if you want to force delete this post!

What do you think about "4 Special Ways to Unpublish a Post Programmatically in WordPress!"?

Your rating submit was successfull.

Your rating submit was not successfull.