Archived topic
Category Archive so no content when post is created under custom post types
17 replies · Started by Gerrit on April 2, 2023
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
Hi there,
can i see one of your post type archive pages ?
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.
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.
No Luck, still the same. Would you like to take a look? I’ve shared my login details in the private information section.
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.
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.
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
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.
Are you expecting to see all Services CPTs in this Category archive ?
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.
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/
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.
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' );
#1 solved ;-) Thank you so much.