[Support request] Category Archive so no content when post is created under custom post types

Home Forums Support [Support request] Category Archive so no content when post is created under custom post types

Home Forums Support Category Archive so no content when post is created under custom post types

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2592714
    Gerrit

    Hi,

    I’ve set up custom post types to my blog for:

    Services
    Destinations
    Activities
    Resources
    Travel Planning

    I’ve set: “Has archive” to “TRUE” and Capability Type to “Post”

    I’ve used the “Explore” starter site from the Site Library on Generate Press to build the website.

    Now I’ve created a menu, and they include category pages Like Activities, Services et cetera. When I click on it I see an almost empty page with just a read more button.

    I was expecting archive cards showing up on these “category/archive” pages. But no. On the Explore starter page for the category page “Destination” it does show posts. But not for mine. It seems that if a post is created under a custom post type they are not working the same.

    I see the Archive card element is loading on the page since it’s set to all archives. So that works. But no content is being loaded.

    I tried looking into the settings of the different block but couldn’t find the answer.

    My question is how can I set up my category pages to show the posts that are in that category just like the pre build archive cards element from the “Explore” starter site even though they are not created under posts but under a custom post type. I would like to keep this dynamic so when new content is added, that content is included to the category page as well.

    Right now navigation (especially on mobile) is difficult because these links give now empty pages.

    Thank you for your help.

    Gerrit

    #2593055
    David
    Staff
    Customer Support

    Hi there,

    can i see one of your post type archive pages ?

    #2599927
    Gerrit

    Here is the link to one of them: https://staging.hiddenholland.com/category/activities/

    This is the one from the site starter: https://staging.hiddenholland.com/category/destinations/

    The posts for the first category are a custom post type (has archive set to true)
    The posts for the 2nd category (destinations) come from the site starter and are under regular posts.

    The same elements load on both. But the archive cards is not doing anything on the activities category.

    If you need anymore information please let me know.

    #2600801
    David
    Staff
    Customer Support

    So i don’t see any loop being output on the activities category.
    Can you go to Settings > Permalinks and Click Save Changes, then clear any caches.
    This will rebuild permalinks which is generally required after registered CPTs or Taxonomies.

    Then revisit the page – let me know.

    #2603652
    Gerrit

    No Luck, still the same. Would you like to take a look? I’ve shared my login details in the private information section.

    #2603670
    Fernando
    Customer Support

    Hi Gerrit,

    Does this only occur for the Activities Category? To test, can you try creating a new test category and assign it to your Activities posts and see if they would appear?

    Try with and without a Content template.

    #2604374
    Gerrit

    Hi Ferando, the answer to your question is in my first post. No, activity is just an example it happens with all custom post type. I’ve shared my login details in the previous response via the private information field so you can take a look. None of the suggestions have worked so far and I don’t understand why.

    #2604772
    Fernando
    Customer Support

    The issue is with your Dynamic Data settings. On your Content Template, just set the Data Source of all Dynamic GB Blocks to “Current Post”. Example: https://share.getcloudapp.com/qGuqxwWr

    #2609848
    Gerrit

    Hey Fernando, still no luck. I have changed every possible block to Dynamic data and data source to Current Post. And still empty pages.

    I saw in your screenshot that you could log into my WordPress environment. Have you tested your suggestion? I am not able to replicate it.

    It’s becoming frustrating because I can’t go out of staging without category pages that make sense.

    https://staging.hiddenholland.com/category/services/
    https://staging.hiddenholland.com/category/activities/
    https://staging.hiddenholland.com/category/travel-planning/
    https://staging.hiddenholland.com/category/resources/
    https://staging.hiddenholland.com/category/destinations/amsterdam/

    These are just examples, all show empty pages.

    #2610471
    David
    Staff
    Customer Support

    Are you expecting to see all Services CPTs in this Category archive ?

    https://staging.hiddenholland.com/category/services/

    #2610770
    Gerrit

    Hi David,

    Yes. This is what I am trying to achieve:

    On my category pages (so services, activities, and destinations for example, but also on the sub-menu categories like Amsterdam), I would like to see all my posts for that category.

    For example, I have a custom post type Services (I do that so I can neatly categorize different custom fields, for example) There, I add new services. For categories, I use no specific taxonomy, I just selected the regular WP categories when creating the custom post types.

    I add the category “services” to all services posts.

    When I go to the category page for services from the front end, I see nothing. Or just the text if I add text in the category settings itself. But I can’t seem to design anything else.

    I thought the content archive page would achieve that from the starter site. But maybe only with regular posts. Because in your example with destinations I do see your example content posts. I don’t see how to change the settings so I can see mine, and also get it to work for the others.

    I hope you can help me out here. I feel it’s such a basic question, have cards on a category page, but I can’t figure it out. Thank you.

    #2610789
    David
    Staff
    Customer Support

    OK, so if your CPT is included in that category then you will need to tell WordPress to include the CPT in the category archive query:

    function tg_include_custom_post_types_in_archive_pages( $query ) {
        if ( $query->is_main_query() && ! is_admin() && ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) {
            $query->set( 'post_type', array( 'a-cpt', 'another-cat', 'and-another-cpt', 'and-another' ) );
        }
    }
    add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_archive_pages' );

    This doc explains how to add the PHP Snippet: https://docs.generatepress.com/article/adding-php/

    #2611958
    Gerrit

    Thank you, David, that was it. They now show on each page.

    https://staging.hiddenholland.com/netherlands-travel-planning-services/

    I have two follow-up questions on this:

    1. I would like to be able to sort the cards. Because the “Inherit query from template” is toggled on (this allows, if I understand correctly, this loop to work on every archive page, no matter the CPT, as it seems to do now, but I can not seem to then add a parameter for sorting anymore. Is there another way to achieve this? Maybe with a snippet? I would like to sort A-Z alphabetically.

    2. Can you help with how I can align the button to be always on the same height no matter the length of the post title and/or excerpt?

    Thank you for your help so far. It’s coming together now.

    #2612041
    Fernando
    Customer Support

    Hi Gerrit,

    Let’s address #1 first.

    Update David’s PHP snippet to this:

    function tg_include_custom_post_types_in_archive_pages( $query ) {
        if ( $query->is_main_query() && ! is_admin() && ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) ) {
            $query->set( 'post_type', array( 'a-cpt', 'another-cat', 'and-another-cpt', 'and-another' ) );
    $query->set( 'orderby', 'title' );
            $query->set( 'order', 'ASC' );
        }
    }
    add_action( 'pre_get_posts', 'tg_include_custom_post_types_in_archive_pages' );
    #2612110
    Gerrit

    #1 solved 😉 Thank you so much.

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