Skip to content

Instantly share code, notes, and snippets.

@onurguven
Forked from Dimasmagadan/WP_Query.php
Last active January 20, 2018 01:05
Show Gist options
  • Save onurguven/ead75b00f2d3aa6518bdcbba14319d68 to your computer and use it in GitHub Desktop.
Save onurguven/ead75b00f2d3aa6518bdcbba14319d68 to your computer and use it in GitHub Desktop.
#WordPress query with args
$args = array(
//Author Parameters - Show posts associated with certain author.
'author' => '1,2,3,'
'author_name' => 'luetkemj',
'author__in' => array( 2, 6 ),
'author__not_in' => array( 2, 6 ),
//Category Parameters - Show posts associated with certain categories.
'cat' => 5,
'category_name' => 'staff, news',
'category_name' => 'staff+news',
'category__and' => array( 2, 6 ),
'category__in' => array( 2, 6 ),
'category__not_in' => array( 2, 6 ),
//Tag Parameters - Show posts associated with certain tags.
'tag' => 'cooking',
'tag_id' => 5,
'tag__and' => array( 2, 6),
'tag__in' => array( 2, 6),
'tag__not_in' => array( 2, 6),
'tag_slug__and' => array( 'red', 'blue'),
'tag_slug__in' => array( 'red', 'blue'),
//Taxonomy Parameters - Show posts associated with certain taxonomy.
'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( 103, 115, 206 ),
'include_children' => false,
'operator' => 'NOT IN'
)
),
//Post & Page Parameters - Display content based on post and page parameters.
'p' => 1,
'name' => 'hello-world',
'page_id' => 1,
'pagename' => 'sample-page',
'pagename' => 'contact_us/canada',
'post_parent' => 1,
'post_parent__in' => array(1,2,3)
'post_parent__not_in' => array(1,2,3),
'post__in' => array(1,2,3),
'post__not_in' => array(1,2,3),
'has_password' => true,
'post_password' => 'multi-pass',
'post_type' => 'any',
'post_status' => 'any',
//Pagination Parameters
'posts_per_page' => 10,
'posts_per_archive_page' => 10,
'nopaging' => false,
'paged' => get_query_var('paged'),
'offset' => 3,
'page' => get_query_var('page'),
'ignore_sticky_posts' => false,
//Order & Orderby Parameters - Sort retrieved posts.
'order' => 'DESC',
'orderby' => 'date',
//Date Parameters - Show posts associated with a certain time and date period.
'year' => 2014,
'monthnum' => 4,
'w' => 25,
'day' => 17,
'hour' => 13,
'minute' => 19,
'second' => 30,
'm' => 201404,
'date_query' => array(
array(
'year' => 2014,
'month' => 4
'week' => 31
'day' => 5
'hour' => 2
'minute' => 3
'second' => 36
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
'compare' => '=',
'column' => 'post_date',
'relation' => 'AND',
),
),
//Custom Field Parameters - Show posts associated with a certain custom field.
'meta_key' => 'key',
'meta_value' => 'value',
'meta_value_num' => 10,
'meta_compare' => '=',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => 'blue'
'type' => 'CHAR',
'compare' => '='
),
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:
'perm' => 'readable'
//Caching Parameters
//NOTE Caching is a good thing. Setting these to false is generally not advised.
'cache_results' => true,
'update_post_term_cache' => true,
'update_post_meta_cache' => true,
'no_found_rows' => false,
//Search Parameter
's' => $s,
'exact' => true,
'sentence' => true,
//Post Field Parameters
'fields' => 'ids'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
}
} // endif
wp_reset_postdata();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment