0

On one of my WP sites I display a single post on the homepage. It's hard coded in the index.php like:

If is home then show post with ID X

But now I have the post's content on the homepage but also a separate post page with the post's slug (domain.com/post-name).

My question: How can I eliminate the post page with the post's slug so that Wordpress throws a

Sorry could not find anything here.

Any help or pointers into the right direction would make me really happy.

  • 2
    Do not publish the post. – fuxia Jun 2 '15 at 6:31
  • Hey toscho thanks that's a splendid idea... but do you know any way to achieve this with the post published? I admit, I am a perfectionist :-D... – Stefan Jun 2 '15 at 8:09
  • I fail to see anything "perfect" in publishing the same content twice. – fuxia Jun 2 '15 at 8:19
  • Being a perfectionist in a simple world is not going to get you anywhere, it will just break your head :-). You want to reinvent the wheel because it is not 100% round, yet it cannot be rounder than it already is, and just because you cannot get it rounder, you refuse to use it. – Pieter Goosen Jun 2 '15 at 9:21
0

Your best way would be to redirect to the theme's 404 page in the event that the page being loaded matches the slug that you are referring to. Try this:

function check_undesirable_page(){
    global $post;
    if(is_page() && ($post->post_name=="YOUR-SLUG-HERE")){
        global $wp_query;
        $wp_query->set_404();
        status_header(404);
    }
}
add_action( 'wp', 'check_undesirable_page' );

EDIT: Also a good idea to incorporate the is_page() condition (added above) to avoid undesirable consequences of the page sharing its slug with other items (in case it's a common word)

  • 1
    Thanks James! This is a great solution I should have come up with myself doh. :-) This problem bugged me for ages but now it works perfectly. – Stefan Jun 3 '15 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.