How to redirect a post having “Draft” status to home page

How to redirect a post having “Draft” status to home page

There are usually 3 types of post status.

  1. Draft
  2. Pending Review
  3. Published

 

There is a case when you wish, not to view them and redirect them to a home page or a specific page when someone lands on that page. Appreciate for the flexibility of WordPress.

See examples below:

1. Redirect posts having status “Drafts” to home page

Open single.php file, or if you are using custom posts than open your_custom_post-single.php file and copy and paste the following code:

if($post->post_status == 'draft') {
    wp_redirect( home_url() . "/", 301 );
}

In the above code we are redirecting the “drafts” posts to home page.

 

2. Redirect posts having status “Drafts” to other page

Open single.php file, or if you are using custom posts than open your_custom_post-single.php file and copy and paste the following code:

if($post->post_status == 'draft') {
    wp_redirect( home_url() . "/some-page/", 301 );
}

In the above code we are redirecting the “drafts” posts to a page having name “some-page”.

Related Posts