Skip to content

Instantly share code, notes, and snippets.

@gregtatum
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save gregtatum/f1f6e0a364ad504a03eb to your computer and use it in GitHub Desktop.

Select an option

Save gregtatum/f1f6e0a364ad504a03eb to your computer and use it in GitHub Desktop.
WP_Query.md

WP_Query

WP_Query is the best way to grab posts in Wordpress, and it's a beast to configure. I created a few snippets in my text editor to help me create them quickly without having to navigate the difficult to read documentation. I've shared them here.

new WP_Query(array(
'post_type' => 'post', //or array
'post_status' => "publish", //publish, pending, draft, auto, future, private, inherit, trash, any
// Pagination
'posts_per_page' => 10, // -1 to show all
// Categories & Tags
'cat' => 1, //category id
'category_name' => "live-music",
'tag' => "featured-post",
'tag_id' => 1,
// Order by
'order' => 'DESC', // or ASC
'orderby' => 'post_date',
// none, ID, author, title, name, type, date, modified, parent, rand,
// comment_count, menu_order, meta_value, meta_value_num, post__in
// if "meta_value" is specified, must include the "meta_key" key in the args
'tax_query' => array(
'relation' => 'AND', //AND, OR
array(
'taxonomy' => 'movie_genre',
'field' => 'slug', //term_id, name, slug
'terms' => array( 'action', 'comedy' ),
),
array(
//Nested
'relation' => 'AND',
array(
'taxonomy' => 'actor',
'field' => 'term_id', //term_id, name, slug
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN', //IN, NOT IN, AND
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'wisdom' ),
),
),
),
'meta_query' => array(
'relation' => 'AND', //AND, OR
array(
'meta_key' => 'color',
'meta_value' => 'blue'
),
array(
'key' => 'color',
'value' => 'red',
'compare' => '!=',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
)
));
<?php
new WP_Query(array(
'post_type' => 'post', //or array
'author' => 1,
'author_name' => "cubic",
's' => "Search Term",
'post_status' => "publish", //publish, pending, draft, auto, future, private, inherit, trash, any
// Post variables
'p' => 1, //post id
'page_id' => 1,
'name' => "post-slug",
'pagename' => "page-slug",
'post_parent' => 1,
'post_parent__in' => array(0,1,2),
'post_parent__not_in' => array(0,1,2),
'post__in' => array(0,1,2),
'post__not_in' => array(0,1,2),
// Pagination
'nopaging' => false, // set to true to return all posts
'posts_per_page' => 10, // -1 to show all
'posts_per_archive_page' => 10,
'offset' => 0,
'paged' => 1, //Show posts for this page
'page' => 1, //Show posts on a static front page
'ignore_sticky_posts' => false,
// Categories
'cat' => 1, //category id
'category_name' => "live-music",
'category__and' => array(0,1,2),
'category__in' => array(0,1,2),
'category__not_in' => array(0,1,2),
// Tags
'tag' => "featured-post",
'tag_id' => 1,
'tag__and' => array(0,1,2),
'tag__in' => array(0,1,2),
'tag__not_in' => array(0,1,2),
'tag_slug__and' => array('featured-post', 'sticky'),
'tag_slug__in' => array('featured-post', 'sticky'),
// Order by
'order' => 'DESC', // or ASC
'orderby' => 'post_date',
// none, ID, author, title, name, type, date, modified, parent, rand,
// comment_count, menu_order, meta_value, meta_value_num, post__in
// if "meta_value" is specified, must include the "meta_key" key in the args
'tax_query' => array(
'relation' => 'AND', //AND, OR
array(
'taxonomy' => 'movie_genre',
'field' => 'slug', //term_id, name, slug
'terms' => array( 'action', 'comedy' ),
),
array(
//Nested
'relation' => 'AND',
array(
'taxonomy' => 'actor',
'field' => 'term_id', //term_id, name, slug
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN', //IN, NOT IN, AND
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'wisdom' ),
),
),
),
'meta_query' => array(
'relation' => 'AND', //AND, OR
array(
'meta_key' => 'color',
'meta_value' => 'blue'
),
array(
'key' => 'color',
'value' => 'red',
'compare' => '!=',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
// Date query
"year" => 2014,
"monthnum" => 12,
"w" => 52,
"day" => 30,
"hour" => 23,
"minute" => 60,
"second" => 60,
"m" => 201307, //YearMonth
"date_query" => array(
"year" => 2014,
"month" => 12, //1-12
"week" => 52, //0-53
"day" => 30, //1-31
"hour" => 23, //0-59
"minute" => 59, //0-59
"second" => 59, //0-59
"after" => 'January 1st, 2013', //strtotime() compatible or array
'before' => array( //strtotime() compatible or array
'year' => 2013,
'month' => 2,
'day' => 28,
),
"inclusive" => false, //match exact before/after
"compare" => ">=", //BETWEEN, ==, !=, etc
"column" => 'post_date',
"relation" => "AND" //OR
),
// Advanced
'perm' => 'readable',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment