get_queried_object() returns null on post date archive

get_queried_object() returns null on post date archives (page type is_date()) and the main blog index page (page type is_home()). Is this intentional, or just an oversight?

I was writing a wrapper around get_queried_object() to get the title of the current page, no matter what type of page it is, for use in a theme. I quickly realized that instead of using get_query_object() I should just duplicate the important bits from wp_title(), but before that I came across an interesting issue.

Read More

It appears that get_queried_object() and its root function WP_Query->get_queried_object() return null for a couple of listing types, including the primary posts list output by index.php (page type is_home()) and post archives by date (page type is_date()).

I tested this by throwing the following snippet into a number of template files in a number of locations, always after get_header() and before the_post():

<pre><code><?php
    $queried_object = get_queried_object();
    var_dump( $queried_object );
?></code></pre>

This works perfectly on category archives, tag archives, custom taxonomy archives, and custom post type archives. get_queried_object() returns the query object, which can be used to extract a page title and other useful information.

However, it fails in archive.php for standard post date archives and index.php for the normal blog posts home page list view.

Digging into the source of WP_Query->get_queried_object() reveals something rather unsurprising: there is no check for page type is_home() or page type is_date(), so on those page types $this->queried_object = null; is not updated and the function returns null.

So my question is, is this intended functionality (e.g. are you not supposed to use get_queried_object() on those pages), a technical limitation (is there not a meaningful object to return on those pages), or simply an implementation oversight?

Is there even an equivalent of the custom post type object for the built-in “blog post” post type to display?

Related posts

1 comment

  1. so my question is, is this intended functionality (e.g. are you not
    supposed to use get_queried_object() on those pages), a technical
    limitation (is there not a meaningful object to return on those
    pages), or simply an implementation oversight?

    get_queried_object() is to get the term, author, single post, single custom post type, or page object being queried. Yes this is intentional and what this function was designed to do.

    If you are on a date archive, home page, or search there is no single object being queried.

    Edit:

    Based on the first comment below the OP needs to get the post_type object. The post_type object is different from the queried_object. If you need to get the post_type object on an archive page you can get it from the query_vars.

    global $wp_query;
    
    $post_type_object = get_post_type_object( $wp_query->query_vars['post_type'] );
    

Comments are closed.