Display not found page 404 on specific Post Type

Here is my case, I’m making some experiments with post types, basically I created a post type called Slider where the featured image is will display in the slide.

Now, when user type something like domain.com/slider/slide1, or try to reach the slider post, I want to display a not found page instead of the single page, or the index in case I dont have the post single page.

Read More

Is it possible to achieve?

Solved!

Thanks to Saif Bechan, I could solve my problem,

Basically I used this function

 add_action('template_redirect', 'my_template_redirect');
 function my_template_redirect() {
 global $wp_query, $post;
 if(is_singular('Slider')) 
 {
      $wp_query->set_404();
   }
 }

Where Slider is the Post Type name,

Related posts

Leave a Reply

3 comments

  1. I have a script that wil; display a good 404 page for certain pages. Now I do not yet know what custom post types are, as I am just starting, but I think you can edit the script to fit your needs.

    add_action('template_redirect', 'my_template_redirect');
    function my_template_redirect()
    {
        global $wp_query, $post;
    
        if (is_author() || is_attachment() || is_day() || is_page())
        {
            $wp_query->set_404();
        }
    
        if (is_feed())
        {
            $author     = get_query_var('author_name');
            $attachment = get_query_var('attachment');
            $attachment = (empty($attachment)) ? get_query_var('attachment_id') : $attachment;
            $day        = get_query_var('day');
            $page   = get_query_var('page');
    
            if (!empty($author) || !empty($attachment) || !empty($day) || !empty($page))
            {
                $wp_query->set_404();
                $wp_query->is_feed = false;
            }
        }
    }
    

    Hope you can do something with this this code. I use this in my functions.php to block access to the different parts of the website. They are also blocked for feeds.

  2. You could just copy your index.php file and name it 404.php. Then if a page or attachment page wasn’t found it would use the 404.php page which would just be a copy of the index.php file.