This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
| Simple Custom Field Query: | |
| Display posts where the custom field key is ‘color’, regardless of the custom field value: | |
| $query = new WP_Query( array( 'meta_key' => 'color' ) ); | |
| Display posts where the custom field value is ‘blue’, regardless of the custom field key: | |
| $query = new WP_Query( array( 'meta_value' => 'blue' ) ); | |
| Display page where the custom field value is ‘blue’, regardless of the custom field key: | |
| $args = array( | |
| 'meta_value' => 'blue', |
This file contains hidden or 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
| <?php | |
| // The Query | |
| $the_query = new WP_Query( $args ); | |
| // The Loop | |
| if ( $the_query->have_posts() ) { | |
| echo '<ul>'; | |
| while ( $the_query->have_posts() ) { | |
| $the_query->the_post(); | |
| echo '<li>' . get_the_title() . '</li>'; |
This file contains hidden or 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
| <?php | |
| // the query | |
| $the_query = new WP_Query( $args ); ?> | |
| <?php if ( $the_query->have_posts() ) : ?> | |
| <!-- pagination here --> | |
| <!-- the loop --> | |
| <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?> | |
| <h2><?php the_title(); ?></h2> | |
| <?php endwhile; ?> | |
| <!-- end of the loop --> |
This file contains hidden or 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
| <?php | |
| // The Query | |
| $query1 = new WPQuery( $args ); | |
| // The Loop | |
| while ( $query1->have_posts() ) { | |
| $query1->the_post(); | |
| echo '<li>' . get_the_title() . '</li>'; | |
| } | |
| /* Restore original Post Data |
This file contains hidden or 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
| Simple Taxonomy Query: | |
| Display posts tagged with bob, under people custom taxonomy: | |
| $args = array( | |
| 'post_type' => 'post', | |
| 'tax_query' => array( | |
| array( | |
| 'taxonomy' => 'people', | |
| 'field' => 'slug', | |
| 'terms' => 'bob', | |
| ), |