admin管理员组

文章数量:1125596

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. :(

本文标签: Taxonomy term archive claims there are no postsbut there are How to resolve