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
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' )
);
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:
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:
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',
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,
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',
<?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>';
<?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
$query1 = new WPQuery( $args );
// The Loop
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<li>' . get_the_title() . '</li>';
}
/* Restore original Post Data
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',
),