Skip to content

Instantly share code, notes, and snippets.

@luetkemj
Last active February 6, 2024 14:25
Show Gist options
  • Save luetkemj/2023628 to your computer and use it in GitHub Desktop.
Save luetkemj/2023628 to your computer and use it in GitHub Desktop.
WP: Query $args
<?php
/**
* WordPress Query Comprehensive Reference
* Compiled by luetkemj - luetkemj.com
*
* CODEX: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
* Source: https://core.trac.wordpress.org/browser/tags/3.9/src/wp-includes/query.php
*/
$args = array(
//////Author Parameters - Show posts associated with certain author.
//http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
'author' => '1,2,3,' //(int) - use author id [use minus (-) to exclude authors by ID ex. 'author' => '-1,-2,-3,']
'author_name' => 'luetkemj', //(string) - use 'user_nicename' (NOT name)
'author__in' => array( 2, 6 ), //(array) - use author id (available with Version 3.7).
'author__not_in' => array( 2, 6 ), //(array)' - use author id (available with Version 3.7).
//////Category Parameters - Show posts associated with certain categories.
//http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
'cat' => 5,//(int) - use category id.
'category_name' => 'staff', 'news', //(string) - use category slug (NOT name).
'category__and' => array( 2, 6 ), //(array) - use category id.
'category__in' => array( 2, 6 ), //(array) - use category id.
'category__not_in' => array( 2, 6 ), //(array) - use category id.
//////Tag Parameters - Show posts associated with certain tags.
//http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters
'tag' => 'cooking', //(string) - use tag slug.
'tag_id' => 5, //(int) - use tag id.
'tag__and' => array( 2, 6), //(array) - use tag ids.
'tag__in' => array( 2, 6), //(array) - use tag ids.
'tag__not_in' => array( 2, 6), //(array) - use tag ids.
'tag_slug__and' => array( 'red', 'blue'), //(array) - use tag slugs.
'tag_slug__in' => array( 'red', 'blue'), //(array) - use tag slugs.
//////Taxonomy Parameters - Show posts associated with certain taxonomy.
//http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
//Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)
//This construct allows you to query multiple taxonomies by using the relation parameter in the first (outer) array to describe the boolean relationship between the taxonomy queries.
'tax_query' => array( //(array) - use taxonomy parameters (available with Version 3.1).
'relation' => 'AND', //(string) - Possible values are 'AND' or 'OR' and is the equivalent of running a JOIN for each taxonomy
array(
'taxonomy' => 'color', //(string) - Taxonomy.
'field' => 'slug', //(string) - Select taxonomy term by ('id' or 'slug')
'terms' => array( 'red', 'blue' ), //(int/string/array) - Taxonomy term(s).
'include_children' => true, //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
'operator' => 'IN' //(string) - Operator to test. Possible values are 'IN', 'NOT IN', 'AND'.
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'include_children' => false,
'operator' => 'NOT IN'
)
),
//////Post & Page Parameters - Display content based on post and page parameters.
//http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
'p' => 1, //(int) - use post id.
'name' => 'hello-world', //(string) - use post slug.
'page_id' => 1, //(int) - use page id.
'pagename' => 'sample-page', //(string) - use page slug.
'pagename' => 'contact_us/canada', //(string) - Display child page using the slug of the parent and the child page, separated ba slash
'post_parent' => 1, //(int) - use page id. Return just the child Pages. (Only works with heirachical post types.)
'post_parent__in' => array(1,2,3) //(array) - use post ids. Specify posts whose parent is in an array. NOTE: Introduced in 3.6
'post_parent__not_in' => array(1,2,3), //(array) - use post ids. Specify posts whose parent is not in an array.
'post__in' => array(1,2,3), //(array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts
'post__not_in' => array(1,2,3), //(array) - use post ids. Specify post NOT to retrieve.
//NOTE: you cannot combine 'post__in' and 'post__not_in' in the same query
//////Password Parameters - Show content based on post and page parameters. Remember that default post_type is only set to display posts but not pages.
//http://codex.wordpress.org/Class_Reference/WP_Query#Password_Parameters
'has_password' => true, //(bool) - available with Version 3.9
//true for posts with passwords;
//false for posts without passwords;
//null for all posts with and without passwords
'post_password' => 'multi-pass', //(string) - show posts with a particular password (available with Version 3.9)
//////Type & Status Parameters - Show posts associated with certain type or status.
//http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters
'post_type' => array( //(string / array) - use post types. Retrieves posts by Post Types, default value is 'post';
'post', // - a post.
'page', // - a page.
'revision', // - a revision.
'attachment', // - an attachment. The default WP_Query sets 'post_status'=>'published', but atchments default to 'post_status'=>'inherit' so you'll need to set the status to 'inherit' or 'any'.
'my-post-type', // - Custom Post Types (e.g. movies)
),
//NOTE: The 'any' keyword available to both post_type and post_status queries cannot be used within an array.
'post_type' => 'any', // - retrieves any type except revisions and types with 'exclude_from_search' set to true.
//////Type & Status Parameters - Show posts associated with certain type or status.
//http://codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters
'post_status' => array( //(string / array) - use post status. Retrieves posts by Post Status, default value i'publish'.
'publish', // - a published post or page.
'pending', // - post is pending review.
'draft', // - a post in draft status.
'auto-draft', // - a newly created post, with no content.
'future', // - a post to publish in the future.
'private', // - not visible to users who are not logged in.
'inherit', // - a revision. see get_children.
'trash' // - post is in trashbin (available with Version 2.9).
),
//NOTE: The 'any' keyword available to both post_type and post_status queries cannot be used within an array.
'post_status' => 'any', // - retrieves any status except those from post types with 'exclude_from_search' set to true.
//////Pagination Parameters
//http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
'posts_per_page' => 10, //(int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page'=1 to show all posts. Note if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. Treimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1
'posts_per_archive_page' => 10, //(int) - number of posts to show per page - on archive pages only. Over-rides showposts anposts_per_page on pages where is_archive() or is_search() would be true
'nopaging' => false, //(bool) - show all posts or use pagination. Default value is 'false', use paging.
'paged' => get_query_var('paged'), //(int) - number of page. Show the posts that would normally show up just on page X when usinthe "Older Entries" link.
//NOTE: Use get_query_var('page'); if you want your query to work in a Page template that you've set as your static front page. The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.
'nopaging' => false, // (boolean) - show all posts or use pagination. Default value is 'false', use paging.
'posts_per_page' => 10, // (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts (the 'offset' parameter is ignored with a -1 value). Set the 'paged' parameter if pagination is off after using this parameter.
//Note: if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1
'posts_per_archive_page' => 10, // (int) - number of posts to show per page - on archive pages only. Over-rides posts_per_page and showposts on pages where is_archive() or is_search() would be true.
'offset' => 3, // (int) - number of post to displace or pass over.
// Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. for a workaround see: http://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
// The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.
'paged' => get_query_var('paged'), //(int) - number of page. Show the posts that would normally show up just on page X when usinthe "Older Entries" link.
//NOTE: This whole paging thing gets tricky. Some links to help you out:
// http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query
// http://codex.wordpress.org/Pagination#Troubleshooting_Broken_Pagination
'page' => get_query_var('page'), // (int) - number of page for a static front page. Show the posts that would normally show up just on page X of a Static Front Page.
//NOTE: The query variable 'page' holds the pagenumber for a single paginated Post or Page that includes the <!--nextpage--> Quicktag in the post content.
'ignore_sticky_posts' => false, // (boolean) - ignore sticky posts or not (available with Version 3.1, replaced caller_get_posts parameter). Default value is 0 - don't ignore sticky posts. Note: ignore/exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.
//////Order & Orderby Parameters - Sort retrieved posts.
//http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
'order' => 'DESC', //(string) - Designates the ascending or descending order of the 'orderby' parameter. Default to 'DESC'.
//Possible Values:
//'ASC' - ascending order from lowest to highest values (1, 2, 3; a, b, c).
//'DESC' - descending order from highest to lowest values (3, 2, 1; c, b, a).
'orderby' => 'date', //(string) - Sort retrieved posts by parameter. Defaults to 'date'. One or more options can be passed. EX: 'orderby' => 'menu_order title'
//Possible Values:
//'none' - No order (available with Version 2.8).
//'ID' - Order by post id. Note the captialization.
//'author' - Order by author.
//'title' - Order by title.
//'name' - Order by post name (post slug).
//'date' - Order by date.
//'modified' - Order by last modified date.
//'parent' - Order by post/page parent id.
//'rand' - Random order.
//'comment_count' - Order by number of comments (available with Version 2.9).
//'menu_order' - Order by Page Order. Used most often for Pages (Order field in the EdiPage Attributes box) and for Attachments (the integer fields in the Insert / Upload MediGallery dialog), but could be used for any post type with distinct 'menu_order' values (theall default to 0).
//'meta_value' - Note that a 'meta_key=keyname' must also be present in the query. Note alsthat the sorting will be alphabetical which is fine for strings (i.e. words), but can bunexpected for numbers (e.g. 1, 3, 34, 4, 56, 6, etc, rather than 1, 3, 4, 6, 34, 56 as yomight naturally expect).
//'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also notthat a 'meta_key=keyname' must also be present in the query. This value allows for numericasorting as noted above in 'meta_value'.
//'title menu_order' - Order by both menu_order AND title at the same time. For more info see: http://wordpress.stackexchange.com/questions/2969/order-by-menu-order-and-title
//'post__in' - Preserve post ID order given in the post__in array (available with Version 3.5).
//////Date Parameters - Show posts associated with a certain time and date period.
//http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
'year' => 2014, //(int) - 4 digit year (e.g. 2011).
'monthnum' => 4, //(int) - Month number (from 1 to 12).
'w' => 25, //(int) - Week of the year (from 0 to 53). Uses the MySQL WEEK command. The mode is dependenon the "start_of_week" option.
'day' => 17, //(int) - Day of the month (from 1 to 31).
'hour' => 13, //(int) - Hour (from 0 to 23).
'minute' => 19, //(int) - Minute (from 0 to 60).
'second' => 30, //(int) - Second (0 to 60).
'm' => 201404, //(int) - YearMonth (For e.g.: 201307).
'date_query' => array( //(array) - Date parameters (available with Version 3.7).
//these are super powerful. check out the codex for more comprehensive code examples http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
array(
'year' => 2014, //(int) - 4 digit year (e.g. 2011).
'month' => 4 //(int) - Month number (from 1 to 12).
'week' => 31 //(int) - Week of the year (from 0 to 53).
'day' => 5 //(int) - Day of the month (from 1 to 31).
'hour' => 2 //(int) - Hour (from 0 to 23).
'minute' => 3 //(int) - Minute (from 0 to 59).
'second' => 36 //(int) - Second (0 to 59).
'after' => 'January 1st, 2013', //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'
'before' => array( //(string/array) - Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day'
'year' => 2013, //(string) Accepts any four-digit year. Default is empty.
'month' => 2, //(string) The month of the year. Accepts numbers 1-12. Default: 12.
'day' => 28, //(string) The day of the month. Accepts numbers 1-31. Default: last day of month.
),
'inclusive' => true, //(boolean) - For after/before, whether exact value should be matched or not'.
'compare' => '=', //(string) - Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='
'column' => 'post_date', //(string) - Column to query against. Default: 'post_date'.
'relation' => 'AND', //(string) - OR or AND, how the sub-arrays should be compared. Default: AND.
),
),
//////Custom Field Parameters - Show posts associated with a certain custom field.
//http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
'meta_key' => 'key', //(string) - Custom field key.
'meta_value' => 'value', //(string) - Custom field value.
'meta_value_num' => 10, //(number) - Custom field value.
'meta_compare' => '=', //(string) - Operator to test the 'meta_value'. Possible values are '!=', '>', '>=', '<', or ='. Default value is '='.
'meta_query' => array( //(array) - Custom field parameters (available with Version 3.1).
'relation' => 'AND', //(string) - Possible values are 'AND', 'OR'. The logical relationship between each inner meta_query array when there is more than one. Do not use with a single inner meta_query array.
array(
'key' => 'color', //(string) - Custom field key.
'value' => 'blue' //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN') Using WP < 3.9? Check out this page for details: http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
'type' => 'CHAR', //(string) - Custom field type. Possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. Default value is 'CHAR'. The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
//NOTE: The 'type' DATE works with the 'compare' value BETWEEN only if the date is stored at the format YYYYMMDD and tested with this format.
'compare' => '=' //(string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Default value is '='.
),
array(
'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
)
),
//////Permission Parameters - Display published posts, as well as private posts, if the user has the appropriate capability:
//http://codex.wordpress.org/Class_Reference/WP_Query#Permission_Parameters
'perm' => 'readable' //(string) Possible values are 'readable', 'editable'
//////Caching Parameters
//http://codex.wordpress.org/Class_Reference/WP_Query#Caching_Parameters
//NOTE Caching is a good thing. Setting these to false is generally not advised.
'cache_results' => true, //(bool) Default is true - Post information cache.
'update_post_term_cache' => true, //(bool) Default is true - Post meta information cache.
'update_post_meta_cache' => true, //(bool) Default is true - Post term information cache.
'no_found_rows' => false, //(bool) Default is false. WordPress uses SQL_CALC_FOUND_ROWS in most queries in order to implement pagination. Even when you don’t need pagination at all. By Setting this parameter to true you are telling wordPress not to count the total rows and reducing load on the DB. Pagination will NOT WORK when this parameter is set to true. For more information see: http://flavio.tordini.org/speed-up-wordpress-get_posts-and-query_posts-functions
//////Search Parameter
//http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
's' => $s, //(string) - Passes along the query string variable from a search. For example usage see: http://www.wprecipes.com/how-to-display-the-number-of-results-in-wordpress-search
'exact' => true, //(bool) - flag to make it only match whole titles/posts - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118
'sentence' => true, //(bool) - flag to make it do a phrase search - Default value is false. For more information see: https://gist.github.com/2023628#gistcomment-285118
//////Post Field Parameters
//For more info see: http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter
//also https://gist.github.com/luetkemj/2023628/#comment-1003542
'fields' => 'ids' //(string) - Which fields to return. All fields are returned by default.
//Possible values:
//'ids' - Return an array of post IDs.
//'id=>parent' - Return an associative array [ parent => ID, … ].
//Passing anything else will return all fields (default) - an array of post objects.
//////Filters
//For more information on available Filters see: http://codex.wordpress.org/Class_Reference/WP_Query#Filters
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
// Do Stuff
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
?>
@luetkemj
Copy link
Author

Gist updated.

Thanks to @charlietriplett, @wpsmith, and @stvwlf for keeping an eye on this thing.

If anyone notices anything I've missed or things that have been added since 3.5 let me know so I can keep this up to date!

@philipdowner
Copy link

Perhaps I'm overlooking it, but the meta_query array accepts a 'relation' key which can have a value of either 'AND' or 'OR'.

@abhjiitpingale
Copy link

I want make relation between post query and meta query "OR". like post_type = "post" OR (meta_key = "key" AND meta_value = "value") so how can I do that please help me out

@michael-cannon
Copy link

@manifestcreative You're correct. In class WP_Meta_Query::__construct, a check for $meta_query['relation'] is made. AND is the default if none is set. The options are AND and OR.

@AkaHappyRoadGirl
Copy link

@luetkemj, I must say I'm your number one fan, this page is one of my most consulted. Thanks a lot!!!

@Dimasmagadan
Copy link

this link (line 173)
//Not sure what these do. For more info see: http://codex.wordpress.org/Class_Reference/WP_Query#Post_Field_Parameters

should be changed to this
http://codex.wordpress.org/Class_Reference/WP_Query#Return_Fields_Parameter

What it does is it changes the format of returned result. Say, you can pass 'fields' => 'ids' and it will return just IDs of matching posts.

@luetkemj
Copy link
Author

Bunch of updates per @manifestcreative and @Dimasmagadan comments. Thanks!

@luetkemj
Copy link
Author

Tons of new updates. Bringing the query up to date with things added in 3.5, 3.7, and 3.9.

Holy wow, I was slacking... sorry about that.

@sephelps
Copy link

sephelps commented May 9, 2014

the line 'category_name' => 'staff', 'news', is invalid. Should just be one of those or an array, right?

@luetkemj
Copy link
Author

@sephelps - yep! that was busted. But it is supposed to be a string. You can display posts that have any of the named categories if you wrap the whole thing in a string, commas and all.

Alternately you can display posts that have "all" of the categories if you use a "+" instead of a ","

'staff,news' or 'staff+news',

Gist has been updated.
See this page for more details on the above:
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

@dgamboaestrada
Copy link

Thank you very much

@JamesVanWaza
Copy link

Please add a comma at the end of line 14,

@luetkemj
Copy link
Author

@jgacuca567 - done - thanks for the catch

@brubrant
Copy link

@luetkemj thanks for this awesome Gist. I come here very often!

Just want to say that some pagination parameters are repeated...

@pageii
Copy link

pageii commented Oct 14, 2015

@luetkemj Just want to say thanks for posting. It's very comprehensive and saved me tons.

@s3rgiosan
Copy link

Hi @luetkemj, first of all thanks for this comprehensive list.

Param posts_per_page is duplicated and the first reference has a typo.
It should be Use 'posts_per_page'=-1 to show all posts..
The second reference is correct.

@luetkemj
Copy link
Author

Thanks for the catch @brubrant and @s3rgiosan!

@esemlabel
Copy link

Where is 'post_mime_type' filter?

@luetkemj
Copy link
Author

luetkemj commented May 9, 2016

I am no longer maintaining this gist. Anyone out there who wants to take this on let me know.

EDIT: This gist is now being maintained in this github repo Help wanted!

@khoipro
Copy link

khoipro commented Aug 16, 2016

Featured this gist on https://www.facebook.com/codemienphi/
@luetkemj Let me help you maintaince this gist. I really think it's helpful!

@carminecappetti
Copy link

Hi all.

I've a question to you.

I use 'sentence' => true in my query_posts().

If I search for "paolo rossi" I get some results;
if I search for "prof. paolo rossi" I get some results;
if I search for "prof paolo rossi" I get NO results;
if I search for "rossi paolo" I get No results;

I need to use "Sentence" clausole because I don't want to show useless posts containing only "Rossi" (for example).

So, how to deal with it?
Is there a way to search ignoring punctuation or words order?

@thinkryan
Copy link

thinkryan commented Feb 4, 2017

In the 'meta_query' section it shows:

'value' => 'blue' //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN') Using WP < 3.9? Check out this page for details:

It should have a comma after 'blue' since it's not the last in the array item in the meta_query array.

See below for correction:

'value' => 'blue', //(string/array) - Custom field value (Note: Array support is limited to a compare value of 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN') Using WP < 3.9? Check out this page for details:

@SalvadorCardona
Copy link

Thank you, is very excellent

@alkasingh19
Copy link

Hi,
I have Implemented UI-mask date in my application, functionality not working in Grid when I click tab in cell to go on date cell.
using below template in directives -
template: '<input ui-mask="19/39/2999" ui-grid-editor  ui-mask-raw="false" only-Number ng-keypress="limitToValidDate($event)" placeholder="MM/DD/YYYY" />',
and I have created following cell template for grid column
var serviceFromDateTemplate = '

{{COL_FIELD}}>
';
can someone help me. on grid cell tab event directives seems not working. however it works when I click on cell but as the requirement is to function on Tab even of prev cell.

Thank you!
Alka Singh

@streeetlamp
Copy link

I've run into this gist so many times googling Query args, twice today. <3 Mark

@notrealdev
Copy link

Hi, why 'category_name' => 'staff, news' not works for me? WP 4.8.2

@yofisim
Copy link

yofisim commented Nov 6, 2017

This is genius, thanks!

@luetkemj
Copy link
Author

luetkemj commented Feb 10, 2018

This gist is now being maintained on github Help wanted!

@Anticosti
Copy link

Great work! Thanks!
Would it be relevant to add these:

is_front_page
is_home
is_page
is_post

?

@luetkemj
Copy link
Author

@Anticosti, feel free to open a PR on github to add those in!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment