Skip to content

Instantly share code, notes, and snippets.

@JamesVanWaza
Forked from luetkemj/wp-query-ref.php
Last active August 3, 2016 15:26
Show Gist options
  • Select an option

  • Save JamesVanWaza/4b0099ab846792ccf1c4 to your computer and use it in GitHub Desktop.

Select an option

Save JamesVanWaza/4b0099ab846792ccf1c4 to your computer and use it in GitHub Desktop.
WP Query
<?php
/**
* The WordPress Query class.
* @link http://codex.wordpress.org/Function_Reference/WP_Query
*/
$args = array(
/** Post & Page Parameters */
'p' => 1,
'name' => 'hello-world',
'page_id' => 1,
'pagename' => 'sample-page',
'post_parent' => 1,
'post__in' => array(1, 2, 3),
'post__not_in' => array(1, 2, 3),
/** Author Parameters */
'author' => '1,2,3,',
'author_name' => 'admin',
/** Category Parameters */
'cat' => 1,
'category_name' => 'blog',
'category__and' => array(1, 2),
'category__in' => array(1, 2),
'category__not_in' => array(1, 2),
/** Type & Status Parameters */
'post_type' => 'any',
'post_status' => 'any',
/** Choose ^ 'any' or from below, since 'any' cannot be in an array */
'post_type' => array(
'post',
'page',
'revision',
'attachment',
'my-post-type',
),
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
),
/** Order & Orderby Parameters */
'order' => 'DESC',
'orderby' => 'date',
'ignore_sticky_posts' => false,
'year' => 2012,
'monthnum' => 1,
'w' => 1,
'day' => 1,
'hour' => 12,
'minute' => 5,
'second' => 30,
/** Tag Parameters */
'tag' => 'cooking',
'tag_id' => 5,
'tag__and' => array(1, 2),
'tag__in' => array(1, 2),
'tag__not_in' => array(1, 2),
'tag_slug__and' => array('red', 'blue'),
'tag_slug__in' => array('red', 'blue'),
/** Pagination Parameters */
'posts_per_page' => 10,
'posts_per_archive_page' => 10,
'nopaging' => false,
'paged' => get_query_var('paged'),
'offset' => 3,
/** Custom Field Parameters */
'meta_key' => 'key',
'meta_value' => 'value',
'meta_value_num' => 10,
'meta_compare' => '=',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'type' => 'CHAR',
'compare' => '=',
),
array(
'key' => 'price',
'value' => array(1, 200),
'compare' => 'NOT LIKE',
),
),
/** Taxonomy Parameters */
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'color',
'field' => 'slug',
'terms' => array('red', 'blue'),
'include_children' => true,
'operator' => 'IN',
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array(1, 2, 3),
'include_children' => false,
'operator' => 'NOT IN',
),
),
/** Permission Parameters */
'perm' => 'readable',
/** Parameters relating to caching */
'no_found_rows' => false,
'cache_results' => true,
'update_post_term_cache' => true,
'update_post_meta_cache' => true,
);
$query = new WP_Query($args);
?>
@JamesVanWaza

Copy link
Copy Markdown
Author

Added the missing comma in line 14.

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