This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Returns posts dated December 12, 2012: | |
$query = new WP_Query( 'year=2012&monthnum=12&day=12' ); | |
or: | |
$args = array( | |
'date_query' => array( | |
array( | |
'year' => 2012, | |
'month' => 12, | |
'day' => 12, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display posts sorted by post ‘title’ in a descending order: | |
$args = array( | |
'orderby' => 'title', | |
'order' => 'DESC', | |
); | |
$query = new WP_Query( $args ); | |
Display posts sorted by ‘menu_order’ with a fallback to post ‘title’, in a descending order: | |
$args = array( | |
'orderby' => 'menu_order title', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display x posts per page: | |
$query = new WP_Query( array( 'posts_per_page' => 3 ) ); | |
Display all posts in one page: | |
$query = new WP_Query( array( 'posts_per_page' => -1 ) ); | |
Display all posts by disabling pagination: | |
$query = new WP_Query( array( 'nopaging' => true ) ); | |
Display posts from the 4th one: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display only posts with the ‘draft‘ status: | |
$query = new WP_Query( array( 'post_status' => 'draft' ) ); | |
Display multiple post status: | |
$args = array( | |
'post_status' => array( 'pending', 'draft', 'future' ) | |
); | |
$query = new WP_Query( $args ); | |
Display all attachments: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display only pages: | |
$query = new WP_Query( array( 'post_type' => 'page' ) ); | |
Display ‘any‘ post type (retrieves any type except revisions and types with ‘exclude_from_search’ set to TRUE): | |
$query = new WP_Query( array( 'post_type' => 'any' ) ); | |
Display multiple post types, including custom post types: | |
$args = array( | |
'post_type' => array( 'post', 'page', 'movie', 'book' ) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display only password protected posts: | |
$query = new WP_Query( array( 'has_password' => true ) ); | |
Display only posts without passwords: | |
$query = new WP_Query( array( 'has_password' => false ) ); | |
Display only posts with and without passwords: | |
$query = new WP_Query( array( 'has_password' => null ) ); | |
Display posts with a particular password: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will NOT work | |
$exclude_ids = '1,2,3'; | |
$query = new WP_Query( array( 'post__not_in' => array( $exclude_ids ) ) ); | |
// This WILL work | |
$exclude_ids = array( 1, 2, 3 ); | |
$query = new WP_Query( array( 'post__not_in' => $exclude_ids ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display post by ID: | |
$query = new WP_Query( array( 'p' => 7 ) ); | |
Display page by ID: | |
$query = new WP_Query( array( 'page_id' => 7 ) ); | |
Show post/page by slug | |
$query = new WP_Query( array( 'name' => 'about-my-life' ) ); | |
Display page by slug: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display posts that have one tag, using tag slug: | |
$query = new WP_Query( array( 'tag' => 'cooking' ) ); | |
Display posts that have this tag, using tag id: | |
$query = new WP_Query( array( 'tag_id' => 13 ) ); | |
Display posts that have “either” of these tags: | |
$query = new WP_Query( array( 'tag' => 'bread,baking' ) ); | |
Display posts that have “all” of these tags: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Display posts that have one category (and any children of that category), using category id: | |
$query = new WP_Query( array( 'cat' => 4 ) ); | |
Display posts that have this category (and any children of that category), using category slug: | |
$query = new WP_Query( array( 'category_name' => 'staff' ) ); | |
Display posts that have this category (not children of that category), using category id: | |
$query = new WP_Query( array( 'category__in' => 4 ) ); | |
Display posts that have several categories, using category id: |