Skip to content

Instantly share code, notes, and snippets.

View carlaizumibamford's full-sized avatar
💭
I may be slow to respond.

CARLA I. BAMFORD carlaizumibamford

💭
I may be slow to respond.
View GitHub Profile
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:
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 );
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 );
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',
Show Posts based on a keyword search
$query = new WP_Query( array( 's' => 'keyword' ) );
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',
),
<?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
<?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 -->
<?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>';
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',