You can manage all of this by hooking the ‘pre_get_posts’ action. No need to alter or create theme templates. In the action callback, check that the ‘cat’ query var is one of the 9 category IDs. This is done by placing all nine IDs in an array, then using in_array() to see if the query var matches any of the array elements.
If that check passes, then set the ‘post_type’ query var to ‘endpoints’ or whatever the post type slug is.
Now when someone makes a normal category request, the resulting posts will only be endpoint posts in that category. So if one of the categories is ‘foo’, requesting example.com/category/foo/ will result in all endpoint posts in the ‘foo’ category.
I’m assuming the categories are only used for endpoints, other posts do not use the same categories. If they did, you would not be able to get to them with a category query without some additional coding.
bcworkz,
Thanks so much for the insight – I think I’m beginning to wrap my brain around how to do this now. You’re correct that the categories are only used for these endpoints and I’m glad that you mentioned putting all the category IDs into an array – makes so much sense. I’m still a bit stuck on how to use the in_array() function. Will I need to create separate if statements that will first check for the page id of a page? For example, say the main page for the ‘foo’ category that will display any endpoints that have the ‘foo’ category checked – will I be creating an if statement that specifically checks to see if this is first the ‘foo’ page and then if so, check in the array for any elements that contain the ‘foo’ category?
Again, thanks for the input here!
That’s the thing, you don’t really need those 9 endpoint pages. Instead of permalinks that lead to those pages, the permalinks can be normal category permalinks that would normally load a category or archive template that shows all posts in the category.
By changing post types in ‘pre_get_posts’ when certain categories are requested, the category or archive template is still loaded, but now it shows all endpoints within the requested category. If your theme’s usual category or archive page does not meet your needs, create a custom archive page that is named archive-endpoint.php. This template will automatically be used for all endpoint queries.
Here’s a typical scenario: The user clicks a link where the href is something like example.com/category/blue-endpoint/. In the ‘pre_get_posts’ action, the ‘cat’ query var will be ‘blue-endpoint’ and the ‘post_type’ query var will be ‘post’. Your action callback then checks if the category is one of the nine in an array of category names.
I originally suggested IDs, which involves an extra step. We may as well use names in the array to avoid the need to get the category term ID. If the ‘cat’ query var is one of the nine, set the ‘post_type’ query var to ‘endpoint’, otherwise do nothing.
If there is an archive-endpoint.php template, WP will automatically use it because of the ‘endpoint’ post type as a query var. Otherwise, the usual category or archive template is used. The resulting posts from the query will all be endpoint posts in the ‘blue-endpoint’ category. When the loop is run on the template, all the endpoint posts from the query will be listed.
No need for special pages, the archive-endpoint.php template can handle what ever variations you need for the endpoint post type. If there’s nothing special about endpoint archives, you could just let your theme’s default category or archive template handle the output.
bcworkz,
Super helpful and I appreciate that extra explanation of how archive-endpoint.php can be used for this, which is a file I already had setup so off to a good start.
Thanks for everything!