PHPWordPress

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

0
1.8k
2018 / 05 / 08

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”.

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks