• Resolved Jennyfer37

    (@jennyfer37)


    Hi,
    I’ve selected to show Category in Custom Posts and have two custom posts with the Category “Featured”. They show in “Categories” in the back end (i.e. it shows there are two posts with this category) but when I click view, I get a 404 error (Nothing Found). I’ve created taxonomies too but wanted to use the default “Category” as well because of a slider on my homepage that can only display items from the default category. I’ve reset the permalinks but it’s not made a difference. Site is https://www.thishitchinmummy.co.uk.

    Any help gratefully received!

    Thanks
    Jen

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi,

    You get a 404 error because WordPress is looking for Posts inside the Featured category and since you don’t have posts it returns the error.

    In order to view your Custom Post Types follow the steps below:

    1. Create an empty plugin like this: https://gist.github.com/sareiodata/76f701e01db6685829db

    2. Add the following code to the end of it:

    /*
     * Filter to view Custom Post Types in a defined Post Category - WCK
     */
    
    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
        if(is_category() || is_tag()) {
            $post_type = get_query_var('post_type');
            if($post_type) {
                $post_type = $post_type;
            } else {
                $post_type = array('post','add your post type here'); // replace CPT to your custom post type
            }
            $query->set('post_type',$post_type);
    
        }
        return $query;
    }

    3. Add your post type inside the array.

    4. Install this plugin via FTP (copy it inside wp-content/plugins) or create a zip archive with it and install it via the WordPress plugin upload functionality.

    After you install and activate the plugin go to the Featured category and click on view.

    Let me know if it works for you.

    Best regards,

    Thread Starter Jennyfer37

    (@jennyfer37)

    Thanks for your reply! I’ve followed these instructions and can now see the custom posts within the categories – brilliant!

    Know it’s prob not the place to ask this but any idea how I can now get them to appear in my slider / featured posts sections on my homepage as my theme doesn’t look for custom posts even though they’re ticked with slider / featured categories…

    Thanks
    Jenny

    Thread Starter Jennyfer37

    (@jennyfer37)

    Actually, just noticed – my menu has disappeared on the category archive pages with this code – any ideas?

    Thanks
    Jenny

    Plugin Author Cristian Antohe

    (@sareiodata)

    > Know it’s prob not the place to ask this but any idea how I can now get them to appear in my slider / featured posts sections on my homepage as my theme doesn’t look for custom posts even though they’re ticked with slider / featured categories…

    You need to modify the theme to query for custom post types in the slider. There’s no way around it unfortunately.

    As for the code, it could be a conflict with your menu. Can you send me a link where this is happening?

    iameffex

    (@iameffex)

    My menu has also disappeared on the custom archive pages now. Can anyone assist with this? Otherwise the code is perfect.

    rwclarke

    (@rwclarke)

    @sareiodata thanks so much for that code! However like @iameffex I have lost the menu in the custom archive pages. Do you know what we need to modify in the code?

    Thanks!

    As per this post (https://wordpress.stackexchange.com/questions/57439/displaying-category-archive-of-custom-post-types), a variation of the below worked for me by putting it in the child theme’s functions.php file I was working on!

    function query_post_type($query) {
        $post_types = get_post_types();
    
        if ( is_category() || is_tag()) {
    
            $post_type = get_query_var('actors');
    
            if ( $post_type ) {
                $post_type = $post_type;
            } else {
                $post_type = $post_types;
            }
    
            $query->set('post_type', $post_type);
    
            return $query;
        }
    }
    
    add_filter('pre_get_posts', 'query_post_type');
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom posts not showing in Category archive’ is closed to new replies.