0

I have a custom post type (url), and a custom taxonomy term (url-category) for this post type.

The archive page for this custom post type works fine.

The taxonomy term archive claims there are no posts for the terms, even when there are. On these taxonomy term archive pages, the function get_queried_object() returns something like this (details replaced with placeholders):

WP_Term Object ( [term_id] => 15229 [name] => -- title -- [slug] => -- slug -- [term_group] => 0 [term_taxonomy_id] => 15229 [taxonomy] => url-category [description] => -- desc -- [parent] => 0 [count] => 38 [filter] => raw )

However, have_posts() immediately after get_queried_object() returns false.

In the Wordpress backend, filtering the post type on the taxonomy term works as expected, showing the matching posts.

How to begin to troubleshoot this?

Updates as per questions in the comments:

  • I use template files for archive-url.php and taxonomy-url-category.php. But, reverting to the default template files makes no difference.

  • Flushing the permalinks makes no difference.

  • I use the pre_get_posts hook to update my queries. But, turning this off makes no difference for what happens above.

  • Adding the post type as a query string parameter does make a difference. The URL /url-category/category/?post_type=url uses the archive-url.php template file, but shows only the posts from the category in question.

So, there's some progress there. But I don't understand what's happening.

Another update:

Adding the following to the code that runs on the hook pre_get_posts makes the taxonomy terms show their content:

if ( is_tax( 'url-category' ) ) {
    $query->set( "post_type", "url" );
}   

Still don't know why this is necessary. :(

5
  • can you share more of the code? Are you using pre_get_posts? In your archives are you using a main query or creating a new custom query to override it? Does the problem go away if you add ?post_type=xyz to the taxonomy archive, where xyz is the slug of the CPT?
    – Tom J Nowell
    Feb 6 at 12:46
  • so ensure your theme has a template file named taxonomy-url-category.php or taxonomy.php . If these files don't exist, and also sometimes, issues with taxonomy term archives not displaying posts can be resolved by flushing the rewrite rules -> visiting the Settings > Permalinks Feb 6 at 13:07
  • Thanks both. @TomJNowell, your suggestion of adding the post type slug does make a difference. But what does this mean?
    – MastaBaba
    Feb 6 at 14:12
  • it means a pre_get_posts filter that modifies the post_type field of the queries would probably solve your issue, and that it's a very common problem that should be easy to search for
    – Tom J Nowell
    Feb 6 at 15:29
  • Setting the post_type in pre_get_posts fixes the problem, as per my edit. But it does not deal with the root cause. I searched but did not find this particular problem mentioned elsewhere.
    – MastaBaba
    Feb 7 at 2:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.