Hijacking WordPress conditionals (or when is_page is not a page)

Sometimes you need a WordPress page to not be a page for the purposes of rendering the content on your site. Most recently I came across this while creating a master “tags” archive page to display all tags from all posts.

The problem is when making a new page for this type of content the resulting “page” is not actually a page in the context of WordPress conditions. In my master tags archive example my custom theme has one sidebar.php which conditionally loads an appropriate sidebar as well as changes to the layout in header.php. Without writing overly specific conditions for what registered sidebars appear my sidebar would be displaying content intended for the “pages” on my site and not appropriate for this “page” that is actually part of the blog, specifically another type of “archive” view.

Part of me suspects the solution I use and offer you here is bad practice somehow (or there is a better way I have yet thought of). But mostly I am madman with no regard for rules and just saying “best practice” causes me to break out in hives. Thus I share it here because it works and can spare you making a whole lot of unneeded templates with duplicate structural layout markup.

is_page() becomes is_archive()

WordPress has a global variable $wp_query that ultimately is “the template” and “the Loop”. I have remarked sardonically in my comments when mucking with $wp_query that global $wp_query; // This IS WordPress

Indeed I have read plenty of tips, tricks, and hacks for wordpress that involve convolutions that could be avoided entirely by manipulating $wp_query directly. Be aware that directly manipulating $wp_query could void your warranty and cause sudden death. Only trained professionals willing to break their sites and get yelled at by clients should open this panel. There are no user serviceable parts as they say.

$wp_query is THE object containing all the information necessary for so many of the WordPress functions you use everyday to “function” but more importantly it is the source for what the WordPress community refers to as “The Loop”. At the end of this post, for reference, I have included a dump of the entire $wp_query object for the “master tags template archive page” I mentioned at the beginning.

Among the various member variables contained within $wp_query you find some familiar names…

WP_Query Object
(
...excerpted for clarity...
    [is_single] =>
    [is_preview] =>
    [is_page] => 1
    [is_archive] =>
    [is_date] =>
    [is_year] =>
    [is_month] =>
    [is_day] =>
    [is_time] =>
    [is_author] =>
    [is_category] =>
    [is_tag] =>
    [is_tax] =>
    [is_search] =>
    [is_feed] =>
    [is_comment_feed] =>
    [is_trackback] =>
    [is_home] =>
    [is_404] =>
    [is_comments_popup] =>
    [is_paged] =>
    [is_admin] =>
    [is_attachment] =>
    [is_singular] => 1
    [is_robots] =>
    [is_posts_page] =>
    [is_post_type_archive] =>
...
)

Yup, by the same name as the conditional functions you use all the time. When WordPress begins loading it’s stack and performs it’s queries it makes a number of decisions based on the results and then stores all that in $wp_query including what type of page you are on.

So in my case while technically speaking my “all tags archive” is a “page” in literal WordPress context resulting in is_page() and is_singular() being true, this is not actually correct in the context of my theme and site design. In reality I need this page to load as if it were a “tag archive”.

With unabashed confidence, like a colonizing imperial nation, I simply change the context stored within $wp_query

global $wp_query;
$wp_query->is_page = $wp_query->is_singular = FALSE;
$wp_query->is_archive = $wp_query->is_tag = TRUE;
get_header();

And voila, my “page” is not treated as “tag archive” from this point forward. Now I need not add/edit my conditions for what content should be rendered in header.php or sidebar.php.

Again, just be aware that doing things like this can have unintended consequences. If you had trouble following the explanation of what is going on here (not accounting for poor writing skills) then you should probably forget you read this and not do things like this.

Reference: $wp_query object

WP_Query Object
(
    [query_vars] => Array
        (
            [page] => 0
            [pagename] => tags
            [error] =>
            [m] => 0
            [p] => 0
            [post_parent] =>
            [subpost] =>
            [subpost_id] =>
            [attachment] =>
            [attachment_id] => 0
            [name] => tags
            [static] =>
            [page_id] => 0
            [second] =>
            [minute] =>
            [hour] =>
            [day] => 0
            [monthnum] => 0
            [year] => 0
            [w] => 0
            [category_name] =>
            [tag] =>
            [cat] =>
            [tag_id] =>
            [author_name] =>
            [feed] =>
            [tb] =>
            [paged] => 0
            [comments_popup] =>
            [meta_key] =>
            [meta_value] =>
            [preview] =>
            [s] =>
            [sentence] =>
            [fields] =>
            [category__in] => Array
                (
                )

            [category__not_in] => Array
                (
                )

            [category__and] => Array
                (
                )

            [post__in] => Array
                (
                )

            [post__not_in] => Array
                (
                )

            [tag__in] => Array
                (
                )

            [tag__not_in] => Array
                (
                )

            [tag__and] => Array
                (
                )

            [tag_slug__in] => Array
                (
                )

            [tag_slug__and] => Array
                (
                )

            [ignore_sticky_posts] =>
            [suppress_filters] =>
            [cache_results] => 1
            [update_post_term_cache] => 1
            [update_post_meta_cache] => 1
            [post_type] =>
            [posts_per_page] => 10
            [nopaging] =>
            [comments_per_page] => 50
            [no_found_rows] =>
            [order] => DESC
            [orderby] => xxx_posts.post_date DESC
        )

    [tax_query] =>
    [post_count] => 1
    [current_post] => -1
    [in_the_loop] =>
    [comment_count] => 0
    [current_comment] => -1
    [found_posts] => 0
    [max_num_pages] => 0
    [max_num_comment_pages] => 0
    [is_single] =>
    [is_preview] =>
    [is_page] => 1
    [is_archive] =>
    [is_date] =>
    [is_year] =>
    [is_month] =>
    [is_day] =>
    [is_time] =>
    [is_author] =>
    [is_category] =>
    [is_tag] =>
    [is_tax] =>
    [is_search] =>
    [is_feed] =>
    [is_comment_feed] =>
    [is_trackback] =>
    [is_home] =>
    [is_404] =>
    [is_comments_popup] =>
    [is_paged] =>
    [is_admin] =>
    [is_attachment] =>
    [is_singular] => 1
    [is_robots] =>
    [is_posts_page] =>
    [is_post_type_archive] =>
    [query_vars_hash] => 5dc0c47b318d620b960d1a0acbc1e5fe
    [query_vars_changed] =>
    [query] => Array
        (
            [page] =>
            [pagename] => tags
        )

    [queried_object] => stdClass Object
        (
            [ID] => 1803
            [post_author] => 1
            [post_date] => 2011-06-25 18:17:03
            [post_date_gmt] => 2011-06-25 18:17:03
            [post_content] => Placeholder text. No content to be edited, just need the page.
            [post_title] => Tags
            [post_excerpt] =>
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] =>
            [post_name] => tags
            [to_ping] =>
            [pinged] =>
            [post_modified] => 2011-06-25 18:17:03
            [post_modified_gmt] => 2011-06-25 18:17:03
            [post_content_filtered] =>
            [post_parent] => 0
            [guid] => http://beingzoe.com/?page_id=1803
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] =>
            [comment_count] => 0
            [filter] => page
        )

    [queried_object_id] => 1803
    [request] =>  SELECT   xxx_posts.* FROM xxx_posts  WHERE 1=1  AND (xxx_posts.ID = '1803') AND xxx_posts.post_type = 'page'  ORDER BY xxx_posts.post_date DESC
    [posts] => Array
        (
            [0] => stdClass Object
                (
                    [ID] => 1803
                    [post_author] => 1
                    [post_date] => 2011-06-25 18:17:03
                    [post_date_gmt] => 2011-06-25 18:17:03
                    [post_content] => Placeholder text. No content to be edited, just need the page.
                    [post_title] => Tags
                    [post_excerpt] =>
                    [post_status] => publish
                    [comment_status] => open
                    [ping_status] => open
                    [post_password] =>
                    [post_name] => tags
                    [to_ping] =>
                    [pinged] =>
                    [post_modified] => 2011-06-25 18:17:03
                    [post_modified_gmt] => 2011-06-25 18:17:03
                    [post_content_filtered] =>
                    [post_parent] => 0
                    [guid] => http://beingzoe.com/?page_id=1803
                    [menu_order] => 0
                    [post_type] => page
                    [post_mime_type] =>
                    [comment_count] => 0
                    [ancestors] => Array
                        (
                        )

                    [filter] => raw
                )

        )

    [post] => stdClass Object
        (
            [ID] => 1803
            [post_author] => 1
            [post_date] => 2011-06-25 18:17:03
            [post_date_gmt] => 2011-06-25 18:17:03
            [post_content] => Placeholder text. No content to be edited, just need the page.
            [post_title] => Tags
            [post_excerpt] =>
            [post_status] => publish
            [comment_status] => open
            [ping_status] => open
            [post_password] =>
            [post_name] => tags
            [to_ping] =>
            [pinged] =>
            [post_modified] => 2011-06-25 18:17:03
            [post_modified_gmt] => 2011-06-25 18:17:03
            [post_content_filtered] =>
            [post_parent] => 0
            [guid] => http://beingzoe.com/?page_id=1803
            [menu_order] => 0
            [post_type] => page
            [post_mime_type] =>
            [comment_count] => 0
            [ancestors] => Array
                (
                )

            [filter] => raw
        )

)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>