query_vars

query_vars

query_vars is a filter on the query variables used by the WP_Query class (and stored in $wp_request->public_query_vars).

The initial array consists of the following query variables:

  • m
  • p
  • posts
  • w
  • cat
  • withcomments
  • s
  • search
  • exact
  • sentence
  • debug
  • calendar
  • page
  • paged
  • more
  • tb
  • pb
  • author
  • order
  • orderby
  • year
  • monthnum
  • day
  • hour
  • minute
  • second
  • name
  • category_name
  • feed
  • author_name
  • static
  • pagename
  • page_id
  • error
  • comments_popup
  • attachment
  • attachment_id
  • subpost
  • subpost_id
  • preview

Your hook would accept an array, add an item to the array, and return the array.

function add_my_query_var($vars) {
$vars[] = 'my_var';
return $vars;
}

add_filter('query_vars', 'add_my_query_var');
Advertisements

One thought on “query_vars

  1. Hello, I would also like to add query into this code to sort by the number of comments, i.e. posts also get sorted by how many comments.

    Thanking you in advance !! 🙂

    // Modify Search Parameters
    function change_wp_search_size($query) {
    if ( $query->is_search )
    $query->query_vars[‘posts_per_page’] = 10;
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘change_wp_search_size’);

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s