[Support request] Custom Post Type Not Displaying Properly on Category/Tag Archives

Home Forums Support [Support request] Custom Post Type Not Displaying Properly on Category/Tag Archives

Home Forums Support Custom Post Type Not Displaying Properly on Category/Tag Archives

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #1585117
    Goran

    The problem:

    – Custom post type messes up the normal display of my category and tag archive structure
    – Instead of 2 column structure and meta being displayed as: img > category link > title > author
    – It gives each entry it’s own line and the meta is reduced to: img > category > title
    – Also, on category and tag archives, the thumbnail from entry from the custom post is showing up as around 1.5x bigger than others + left-aligned, and the meta is to the right.
    – But all the other category archive entries have the img on top of the other meta
    – note on the above: I do have a code in my function.php to “add avatar to author byline” but removing it does nothing
    – custom post types share categories and tags with default post types.

    Things I’ve done so far:

    – created a custom post type

    function my_custom_post() {
        
        $args = array(
    
            'labels' => array(
                'name' => 'My CPTS',
                'singular_name' => 'My CPT',
    
            ),
            'hierarchical' => false,
            'public' => true,
            'has_archive' => true,
            'menu_icon' => 'dashicons-universal-access',
            'supports' => array(
                'title',
                'editor',
                'author',
                'thumbnail',
                'excerpt',
                'author',
                'custom-fields',
                'comments',
                'revisions',
                'post-formats'
            ),
            'taxonomies' => array('category', 'post_tag'),
            'show_in_rest' => true
            //'rewrite' => array('slug' => 'cpt'), <- I could make this whatever I want it to say
    
        );
    
        register_post_type('my-cpt', $args);
    }
    
    add_action('init', 'my_custom_post');

    – hooked into the query to show custom post types in home and category archives

    function add_cpt_to_category_archive( $query ) {
      if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_archive() || is_category() || is_home() ) {
          // Add CPT to the category
          $query->set( 'post_type', array( 'post', 'my-cpt' ) );
        }
        if ( $query->is_search() ) {
          // Add CPT to the search
          $query->set( 'post_type', array( 'post', 'my-cpt' ) );
        }
      }
    }
    add_action('pre_get_posts', 'add_cpt_to_category_archive' ); 

    What I’m looking to achieve:

    – custom post types being fed into the category/home/search archives consistent with default post types layout/style
    – custom post types and default post types to show up together in archives and search queries

    #1585816
    David
    Staff
    Customer Support

    Hi there,

    can you share a link to where we can see the problem?

    #1586211
    Goran

    Sure, see pm.

    The Test Post is the custom type one.

    I’m also seeing that the custom post type archive includes non-custom post type posts.

    See link #3.

    #1586462
    Tom
    Lead Developer
    Lead Developer

    Let’s tackle the columns issue first. Try this:

    add_filter( 'generate_blog_columns', function( $columns ) {
        if ( ! is_singular() && 'your-cpt' === get_post_type() ) {
            return true;
        }
    
        return $columns;
    } );

    Does that provide consistent results?

    Let us know πŸ™‚

    #1586464
    Goran

    Yes that fixes the issue of the column.

    #1587420
    Tom
    Lead Developer
    Lead Developer

    Awesome. So what’s left to figure out? You’re wanting to add post meta to these cpt items as well?

    Let me know πŸ™‚

    #1587428
    Goran

    Yes, the post meta is acting strangely. For example

    When the CPT is the latest post in the loop, then all the meta gets stripped from all posts in that archive. Example: https://theeldergeek.com/movies/

    But when it is not the latest item in the loop, the meta does not get stripped.
    Example: https://theeldergeek.com/shows/

    #1587735
    Goran

    Also:
    – on category and tag archives, the thumbnail from entry from the custom post is showing up as around 1.5x bigger than others
    – additionally, the custom post type category ‘geek-bios’ show non-custom post types as well. Is there a way to fix this to show only custom post types without creating a separate custom post type archive page?

    #1588399
    Tom
    Lead Developer
    Lead Developer

    You will likely need to enable post meta on your CPTs:
    https://docs.generatepress.com/article/generate_entry_meta_post_types/
    https://docs.generatepress.com/article/generate-footer-meta-post-types/

    As for the thumbnail – where are you defining the size?

    For the CPT archive – it will only show posts from its CPT if it’s set up correctly. If that’s not the case, perhaps you’re using a function somewhere altering the query?

    #1589697
    Goran

    You will likely need to enable post meta on your CPT. – Worked

    As for the thumbnail – where are you defining the size? Nowhere. Assumed it would inherit.

    For the CPT archive. – two relevant functions are at the top of this thread. I will comb through the files again to see if I changed/added anything else anywhere,

    #1589774
    Tom
    Lead Developer
    Lead Developer

    It should inherit the image size from Customize > Layout > Blog – what do you have set up as your image values in there?

    Let us know πŸ™‚

    #1589819
    Goran

    Well, that helped me realize that as usual, it was my fault.
    The test post feat img was of a diff size than the others. Thanks!

    Everything is solved now except for the CPT Archive.

    Basically, it’s being treated as All Post and not isolating only my CPT published posts.

    I’ve combed through the files and can’t see anything that I’ve changed anywhere pertinent.

    #1591058
    Tom
    Lead Developer
    Lead Developer

    The CPT archive should only contain posts from that CPT by default – that’s just how WordPress works. Perhaps one of your plugins is interfering with the query?

    #1591215
    Goran

    Hi Tom,

    Is that the case even if I don’t have a separate archive for the CPTs and/or don’t have a separate taxonomy setup for them either (currently just have ‘taxonomies’ => array(‘category’, ‘post_tag’)?

    #1591247
    Goran

    Hey Tom, more info I just came across.

    //Hooks into the query to show custom post types into the category archive

    function add_cpt_to_category_archive( $query ) {
      if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_archive() || is_category() || is_home() ) {
             // Add CPT to the category
             $query->set( 'post_type', array( 'post', 'geek-bios' ) );
        }  
        if ( $query->is_search() ) {
             // Add CPT to the search
             $query->set( 'post_type', array( 'post', 'geek-bios' ) );
        } 
      }
    }
    
    add_action('pre_get_posts', 'add_cpt_to_category_archive' );

    I removed is_archive() from the above 2nd if statement and it seems to have done the trick.

    Also removed the 3rd if statement as it is showing up in search even without that code.

    Thoughts?

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.