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
Show Posts without adding post information to the cache | |
Display 50 posts, but don’t add post information to the cache | |
$args = array( | |
'posts_per_page' => 50, | |
'cache_results' => false | |
); | |
$query = new WP_Query( $args ); | |
Show Posts without adding post meta information to the cache | |
Display 50 posts, but don’t add post meta information to the cache: |
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
Get attachments that are gif images: | |
Get gif images and remember that by default the attachment’s post_status is set to inherit. | |
$args = array( | |
'post_type' => 'attachment', | |
'post_status' => 'inherit', | |
'post_mime_type' => 'image/gif', | |
); | |
$query = new WP_Query( $args ); |
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 published and private posts, if the user has the appropriate capability: | |
$args = array( | |
'post_status' => array( 'publish', 'private' ), | |
'perm' => 'readable', | |
); | |
$query = new WP_Query( $args ); |
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 with 20 comments: | |
$args = array( | |
'post_type' => 'post', | |
'comment_count' => 20, | |
); | |
$query = new WP_Query( $args ); | |
Display posts with 25 comments or more: | |
$args = array( | |
'post_type' => 'post', |
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
Show Posts based on a keyword search | |
$query = new WP_Query( array( 's' => 'keyword' ) ); |
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
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', | |
), |
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
<?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 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 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 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', |