WordPress by example: $wp_query

We should be familiar with the concept of the loop. As demonstrated below, the WordPress template logic will continue to get posts until there are no more. With each iteration of the loop, we get the post using a SQL query and save the retrieve information int the global variable $wp_query.

<?php
if ( have_posts() ) {
    while ( have_posts() ) {
        the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <?php the_content(); ?>
    <?php }
}

Notice that we do not need to pass any parameters to the_title() nor the_content() because they will get there information by default from $wp_query. For example, the_title() will display the contents of $wp_query[“post”]->post_title.

The entire $wp_query array is displayed below so you can see all the variables. Depending on the type of query that was run, some variables will be more or less used. The most useful information has been moved just below.

    ["in_the_loop"]=> bool(true)    
    ["post"]=> object(WP_Post)#2443 (24) 
    { 
        ["ID"]=> int(1874) 
        ["post_author"]=> string(1) "1" 
        ["post_date"]=> string(19) "2020-05-31 15:08:38" 
        ["post_date_gmt"]=> string(19) "2020-05-31 19:08:38" 
        ["post_content"]=> string(4242) "
            the_category() function
            The function the_category() is frequently {snip} 
        " 
        ["post_title"]=> string(51) "WordPress by example: the_category() and the_tags()" 
        ["post_excerpt"]=> string(0) "" 
        ["post_status"]=> string(7) "publish" 
        ["comment_status"]=> string(6) "closed" 
        ["ping_status"]=> string(6) "closed" 
        ["post_password"]=> string(0) "" 
        ["post_name"]=> string(46) "wordpress-by-example-the_category-and-the_tags" 
        ["to_ping"]=> string(0) "" 
        ["pinged"]=> string(0) "" 
        ["post_modified"]=> string(19) "2020-05-31 16:59:19" 
        ["post_modified_gmt"]=> string(19) "2020-05-31 20:59:19" 
        ["post_content_filtered"]=> string(0) "" 
        ["post_parent"]=> int(0) 
        ["guid"]=> string(38) "https://www.cheetahwheelies.com/?p=1874" 
        ["menu_order"]=> int(0) 
        ["post_type"]=> string(4) "post" 
        ["post_mime_type"]=> string(0) "" 
        ["comment_count"]=> string(1) "0" 
        ["filter"]=> string(3) "raw" 
    }
    ["is_single"]=> bool(true) 
    ["is_preview"]=> bool(false) 
    ["is_page"]=> bool(false) 
    ["is_archive"]=> bool(false) 
    ["is_date"]=> bool(false) 
    ["is_year"]=> bool(false) 
    ["is_month"]=> bool(false) 
    ["is_day"]=> bool(false) 
    ["is_time"]=> bool(false) 
    ["is_author"]=> bool(false) 
    ["is_category"]=> bool(false) 
    ["is_tag"]=> bool(false) 
    ["is_tax"]=> bool(false) 
    ["is_search"]=> bool(false) 
    ["is_feed"]=> bool(false) 
    ["is_comment_feed"]=> bool(false) 
    ["is_trackback"]=> bool(false) 
    ["is_home"]=> bool(false) 
    ["is_privacy_policy"]=> bool(false) 
    ["is_404"]=> bool(false) 
    ["is_embed"]=> bool(false) 
    ["is_paged"]=> bool(false) 
    ["is_admin"]=> bool(false) 
    ["is_attachment"]=> bool(false) 
    ["is_singular"]=> bool(true) 
    ["is_robots"]=> bool(false) 
    ["is_favicon"]=> bool(false) 
    ["is_posts_page"]=> bool(false) 
    ["is_post_type_archive"]=> bool(false) 
    } 

The complete unaltered query:

object(WP_Query)#1550 (53) 
{ 
    ["query"]=> array(2) { 
        ["page"]=> string(0) "" 
        ["name"]=> string(46) "wordpress-by-example-the_category-and-the_tags"
    }     
    ["query_vars"]=> array(64) 
    { 
        ["page"]=> int(0) 
        ["name"]=> string(46) "wordpress-by-example-the_category-and-the_tags" 
        ["error"]=> string(0) "" 
        ["m"]=> string(0) "" 
        ["p"]=> int(0) 
        ["post_parent"]=> string(0) "" 
        ["subpost"]=> string(0) "" 
        ["subpost_id"]=> string(0) "" 
        ["attachment"]=> string(0) "" 
        ["attachment_id"]=> int(0) 
        ["pagename"]=> string(0) "" 
        ["page_id"]=> int(0) 
        ["second"]=> string(0) "" 
        ["minute"]=> string(0) "" 
        ["hour"]=> string(0) "" 
        ["day"]=> int(0) 
        ["monthnum"]=> int(0) 
        ["year"]=> int(0) 
        ["w"]=> int(0) 
        ["category_name"]=> string(0) "" 
        ["tag"]=> string(0) "" 
        ["cat"]=> string(0) "" 
        ["tag_id"]=> string(0) "" 
        ["author"]=> string(0) "" 
        ["author_name"]=> string(0) "" 
        ["feed"]=> string(0) "" 
        ["tb"]=> string(0) "" 
        ["paged"]=> int(0) 
        ["meta_key"]=> string(0) "" 
        ["meta_value"]=> string(0) "" 
        ["preview"]=> string(0) "" 
        ["s"]=> string(0) "" 
        ["sentence"]=> string(0) "" 
        ["title"]=> string(0) "" 
        ["fields"]=> string(0) "" 
        ["menu_order"]=> string(0) "" 
        ["embed"]=> string(0) "" 
        ["category__in"]=> array(0) { } 
        ["category__not_in"]=> array(0) { } 
        ["category__and"]=> array(0) { } 
        ["post__in"]=> array(0) { } 
        ["post__not_in"]=> array(0) { } 
        ["post_name__in"]=> array(0) { } 
        ["tag__in"]=> array(0) { } 
        ["tag__not_in"]=> array(0) { } 
        ["tag__and"]=> array(0) { } 
        ["tag_slug__in"]=> array(0) { } 
        ["tag_slug__and"]=> array(0) { } 
        ["post_parent__in"]=> array(0) { } 
        ["post_parent__not_in"]=> array(0) { } 
        ["author__in"]=> array(0) { } 
        ["author__not_in"]=> array(0) { } 
        ["ignore_sticky_posts"]=> bool(false) 
        ["suppress_filters"]=> bool(false) 
        ["cache_results"]=> bool(true) 
        ["update_post_term_cache"]=> bool(true) 
        ["lazy_load_term_meta"]=> bool(true) 
        ["update_post_meta_cache"]=> bool(true) 
        ["post_type"]=> string(0) "" 
        ["posts_per_page"]=> int(10) 
        ["nopaging"]=> bool(false) 
        ["comments_per_page"]=> string(2) "20" 
        ["no_found_rows"]=> bool(false) 
        ["order"]=> string(4) "DESC" 
    }   
    ["tax_query"]=> NULL 
    ["meta_query"]=> object(WP_Meta_Query)#2404 (9) 
    { 
        ["queries"]=> array(0) { } 
        ["relation"]=> NULL 
        ["meta_table"]=> NULL 
        ["meta_id_column"]=> NULL 
        ["primary_table"]=> NULL 
        ["primary_id_column"]=> NULL 
        ["table_aliases":protected]=> array(0) { } 
        ["clauses":protected]=> array(0) { } 
        ["has_or_relation":protected]=> bool(false) 
    } 
    ["date_query"]=> bool(false)
    ["queried_object"]=> object(WP_Post)#2443 (24)
    { 
        ["ID"]=> int(1874) 
        ["post_author"]=> string(1) "1" 
        ["post_date"]=> string(19) "2020-05-31 15:08:38" 
        ["post_date_gmt"]=> string(19) "2020-05-31 19:08:38" 
        ["post_content"]=> string(4242) "
            the_category() function
            The function the_category() is frequently {snip} 
        " 
        ["post_title"]=> string(51) "WordPress by example: the_category() and the_tags()" 
        ["post_excerpt"]=> string(0) "" 
        ["post_status"]=> string(7) "publish" 
        ["comment_status"]=> string(6) "closed" 
        ["ping_status"]=> string(6) "closed" 
        ["post_password"]=> string(0) "" 
        ["post_name"]=> string(46) "wordpress-by-example-the_category-and-the_tags" 
        ["to_ping"]=> string(0) "" 
        ["pinged"]=> string(0) "" 
        ["post_modified"]=> string(19) "2020-05-31 16:59:19" 
        ["post_modified_gmt"]=> string(19) "2020-05-31 20:59:19" 
        ["post_content_filtered"]=> string(0) "" 
        ["post_parent"]=> int(0) 
        ["guid"]=> string(38) "https://www.cheetahwheelies.com/?p=1874" 
        ["menu_order"]=> int(0) 
        ["post_type"]=> string(4) "post" 
        ["post_mime_type"]=> string(0) "" 
        ["comment_count"]=> string(1) "0" 
        ["filter"]=> string(3) "raw" 
    }
    ["queried_object_id"]=> int(1874) 
    ["request"]=> string(211) "SELECT wp_apjc_posts.* FROM wp_apjc_posts WHERE 1=1 AND wp_apjc_posts.post_name = 'wordpress-by-example-the_category-and-the_tags' AND wp_apjc_posts.post_type = 'post' ORDER BY wp_apjc_posts.post_date DESC "  
    ["posts"]=> &array(1) 
    { 
        [0]=> object(WP_Post)#2443 (24) { 
            ["ID"]=> int(1874) 
            ["post_author"]=> string(1) "1" 
            ["post_date"]=> string(19) "2020-05-31 15:08:38" 
            ["post_date_gmt"]=> string(19) "2020-05-31 19:08:38" 
            ["post_content"]=> string(4242)  "
            the_category() function
            The function the_category() is frequently {snip} 
        " 
            ["post_title"]=> string(51) "WordPress by example: the_category() and the_tags()" 
            ["post_excerpt"]=> string(0) "" 
            ["post_status"]=> string(7) "publish" 
            ["comment_status"]=> string(6) "closed" 
            ["ping_status"]=> string(6) "closed" 
            ["post_password"]=> string(0) "" 
            ["post_name"]=> string(46) "wordpress-by-example-the_category-and-the_tags" 
            ["to_ping"]=> string(0) "" 
            ["pinged"]=> string(0) "" 
            ["post_modified"]=> string(19) "2020-05-31 16:59:19" 
            ["post_modified_gmt"]=> string(19) "2020-05-31 20:59:19" 
            ["post_content_filtered"]=> string(0) "" 
            ["post_parent"]=> int(0) 
            ["guid"]=> string(38) "https://www.cheetahwheelies.com/?p=1874" 
            ["menu_order"]=> int(0) 
            ["post_type"]=> string(4) "post" 
            ["post_mime_type"]=> string(0) "" 
            ["comment_count"]=> string(1) "0" 
            ["filter"]=> string(3) "raw" 
        } 
    }   
    ["post_count"]=> int(1)     
    ["current_post"]=> int(0)   
    ["in_the_loop"]=> bool(true)    
    ["post"]=> object(WP_Post)#2443 (24) 
    { 
        ["ID"]=> int(1874) 
        ["post_author"]=> string(1) "1" 
        ["post_date"]=> string(19) "2020-05-31 15:08:38" 
        ["post_date_gmt"]=> string(19) "2020-05-31 19:08:38" 
        ["post_content"]=> string(4242) "
            the_category() function
            The function the_category() is frequently {snip} 
        " 
        ["post_title"]=> string(51) "WordPress by example: the_category() and the_tags()" 
        ["post_excerpt"]=> string(0) "" 
        ["post_status"]=> string(7) "publish" 
        ["comment_status"]=> string(6) "closed" 
        ["ping_status"]=> string(6) "closed" 
        ["post_password"]=> string(0) "" 
        ["post_name"]=> string(46) "wordpress-by-example-the_category-and-the_tags" 
        ["to_ping"]=> string(0) "" 
        ["pinged"]=> string(0) "" 
        ["post_modified"]=> string(19) "2020-05-31 16:59:19" 
        ["post_modified_gmt"]=> string(19) "2020-05-31 20:59:19" 
        ["post_content_filtered"]=> string(0) "" 
        ["post_parent"]=> int(0) 
        ["guid"]=> string(38) "https://www.cheetahwheelies.com/?p=1874" 
        ["menu_order"]=> int(0) 
        ["post_type"]=> string(4) "post" 
        ["post_mime_type"]=> string(0) "" 
        ["comment_count"]=> string(1) "0" 
        ["filter"]=> string(3) "raw" 
    } 
    ["comment_count"]=> int(0) 
    ["current_comment"]=> int(-1) 
    ["found_posts"]=> int(1) 
    ["max_num_pages"]=> int(0) 
    ["max_num_comment_pages"]=> int(0) 
    ["is_single"]=> bool(true) 
    ["is_preview"]=> bool(false) 
    ["is_page"]=> bool(false) 
    ["is_archive"]=> bool(false) 
    ["is_date"]=> bool(false) 
    ["is_year"]=> bool(false) 
    ["is_month"]=> bool(false) 
    ["is_day"]=> bool(false) 
    ["is_time"]=> bool(false) 
    ["is_author"]=> bool(false) 
    ["is_category"]=> bool(false) 
    ["is_tag"]=> bool(false) 
    ["is_tax"]=> bool(false) 
    ["is_search"]=> bool(false) 
    ["is_feed"]=> bool(false) 
    ["is_comment_feed"]=> bool(false) 
    ["is_trackback"]=> bool(false) 
    ["is_home"]=> bool(false) 
    ["is_privacy_policy"]=> bool(false) 
    ["is_404"]=> bool(false) 
    ["is_embed"]=> bool(false) 
    ["is_paged"]=> bool(false) 
    ["is_admin"]=> bool(false) 
    ["is_attachment"]=> bool(false) 
    ["is_singular"]=> bool(true) 
    ["is_robots"]=> bool(false) 
    ["is_favicon"]=> bool(false) 
    ["is_posts_page"]=> bool(false) 
    ["is_post_type_archive"]=> bool(false) 
    ["query_vars_hash":"WP_Query":private]=> string(32) "820086345072b1c9c8be45502f7010f8" 
    ["query_vars_changed":"WP_Query":private]=> bool(false) 
    ["thumbnails_cached"]=> bool(false) 
    ["stopwords":"WP_Query":private]=> NULL 
    ["compat_fields":"WP_Query":private]=> array(2) 
    { 
        [0]=> string(15) "query_vars_hash" 
        [1]=> string(18) "query_vars_changed"
    } 
    ["compat_methods":"WP_Query":private]=> array(2)
    { 
        [0]=> string(16) "init_query_flags" 
        [1]=> string(15) "parse_tax_query" 
    }
}