Basic usage:
<?php
$args = array(
// Arguments for your query.
);
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
// Contents of the queried post results go here.
}
}
// Restore original post data.
wp_reset_postdata();
?>
Argument array
cat (int): use category id.
category_name (string): use category slug (NOT name).
category__and (array): use category id.
category__in (array): use category id.
category__not_in (array): use category id.
// tag
tag (string): use tag slug.
tag_id (int): use tag id.
tag__and (array): use tag ids.
tag__in (array): use tag ids.
tag__not_in (array): use tag ids.
tag_slug__and (array): use tag slugs.
tag_slug__in (array): use tag slugs.
// author
author (int) – use author id.
author_name (string) – use ‘user_nicename‘ – NOT name.
author__in (array) – use author id (available since version 3.7).
author__not_in (array) – use author id (available since version 3.7).
// taxonomy
tax_query (array) – use taxonomy parameters (available since version 3.1).
relation (string) – The logical relationship between each inner taxonomy array when there is more than one. Possible values are ‘AND’, ‘OR’. Do not use with a single inner taxonomy array.
taxonomy (string) – Taxonomy.
field (string) – Select taxonomy term by. Possible values are ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’. Default value is ‘term_id’.
terms (int/string/array) – Taxonomy term(s).
include_children (boolean) – Whether or not to include children for hierarchical taxonomies. Defaults to true.
operator (string) – Operator to test. Possible values are ‘IN’, ‘NOT IN’, ‘AND’, ‘EXISTS’ and ‘NOT EXISTS’. Default value is ‘IN’.
Return
$query
Holds the query string that was passed to the $wp_query object by WP class.
$query_vars
An associative array containing the dissected $query: an array of the query variables and their respective values.
$queried_object
Applicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page.
$queried_object_id
If the request is a category, author, permalink or post / page, holds the corresponding ID.
$posts
Gets filled with the requested posts from the database.
$post_count
The number of posts being displayed.
$found_posts
The total number of posts found matching the current query parameters
$max_num_pages
The total number of pages. Is the result of $found_posts / $posts_per_page
$current_post
(available during The Loop) Index of the post currently being displayed.
$post
(available during The Loop) The post currently being displayed.
$is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, $is_comments_popup, $is_admin, $is_attachment, $is_singular, $is_robots, $is_posts_page, $is_paged
Booleans dictating what type of request this is. For example, the first three represent ‘is it a permalink?’, ‘is it a Page?’, ‘is it any type of archive page?’, respectively. See also Conditional Tags.