• Hi,

    I have a custom post type (“knowledge”) archive page that I want to be filterable by taxonomy (“topic”) as well as a custom field (“type”). To hook up the custom field filtering I’m using pre_get_posts to add a meta_query. Since I only want this filtering to be available on this archive page, I first check that $query->is_main_query() == true && is_archive() && get_queried_object()->name == "knowledge".

    The problem I’ve encountered is that get_queried_object() returns the taxonomy object if “topic” is in the URL and the post type object of the archive otherwise. I think the underlying issue is that a query can satisfy both is_archive and is_tax at the same time, so get_queried_object() has no way of knowing which one I want.

    If I want the “topic” taxonomy to be publicly queryable, how can I reliably check the post type of an archive page?

Viewing 1 replies (of 1 total)
  • Thread Starter mslade

    (@mslade)

    On further review it looks like WP_Query‘s internal $queried_object can be set in two places:

    • During parse_query() if a pagename is provided; that page is used.
    • In get_queried_object():
    • if a category, tag, or custom taxonomy term is provided, that term is used
    • else if this is an archive request, the archive post type is used.
    • else if this is the posts page, that posts page is used.
    • else if this is any single post, that post is used.
    • else if this is an author page, that author is used.

    Based on that, and because a query can satisfy both is_tax and is_post_type_archive, it appears that get_queried_object cannot be reliably used to determine the post type of an archive page.

Viewing 1 replies (of 1 total)
  • The topic ‘Ambiguous get_queried_object()’ is closed to new replies.