Cyber Monday Sale!

Save up to $50 through December 5th.

Shop Now

[Resolved] Custom Post Type posts not showing on home or archive pages

Home Forums Support [Resolved] Custom Post Type posts not showing on home or archive pages

Home Forums Support Custom Post Type posts not showing on home or archive pages

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #845366
    Matthew

    hello,

    I created a custom post type and two test posts for it but they are not feeding onto my home page (set to display my latest posts under Reading Settings) nor are they being pulled onto the monthly archives page. Only regular Posts are there.

    My question is twofold, 1) Do people aim to get their CPT posts to show up with their other posts if they are in the mould of a blog article (which my CPT’s are). Or are they separated for a reason.

    and 2) I tried a few snippets in my functions.php that I found online but none work so far. Is there a filter for this?

    thanks!

    #845890
    Leo
    Staff
    Customer Support

    Hi there,

    CPTs and post loops are actually handled by WordPress itself and not the theme.

    People generally don’t want CPTs showing up in the default WordPress post loops – which is why they create CPTs.

    I believe this solution should work:
    https://wordpress.stackexchange.com/questions/160814/adding-custom-post-type-to-loop

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #847206
    Matthew

    hi Leo thanks for the link, that code worked.

    For any others who like to know I modified the code there so it would show into both home and date archive:

    function add_custom_post_type_to_query( $query ) {
        if ( ! is_admin()  &&  $query->is_main_query() ) {
           if ( $query->is_date() ||  $query->is_home()  ) {
    
               $query->set( 'post_type', array('post', 'pictures') );
    
           }
        }
    }
    add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );

    People generally don’t want CPTs showing up in the default WordPress post loops – which is why they create CPTs.

    I see. I could use my default posts easily enough instead of a CPT but I want to add custom fields to the edit screen and also customize the template and then these would show up in every post I made when I don’t need them. I thought as well as clutter it could cause other issues.

    thanks,

    #847605
    Leo
    Staff
    Customer Support

    No problem 🙂

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