Skip to content

Instantly share code, notes, and snippets.

@leepettijohn
Created August 28, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leepettijohn/f4034a319a3e535a5d3f78b30ec4bf8d to your computer and use it in GitHub Desktop.
Save leepettijohn/f4034a319a3e535a5d3f78b30ec4bf8d to your computer and use it in GitHub Desktop.
Basic Loop
<?php /**
* The WordPress Query class.
* @link http://codex.wordpress.org/Function_Reference/WP_Query
*
*/
$args = array(
//Post & Page Parameters
'p' => 1,
'name' => 'hello-world',
'page_id' => 1,
'pagename' => 'sample-page',
'post_parent' => 1,
'post__in' => array(1,2,3),
'post__not_in' => array(1,2,3),
//Author Parameters
'author' => '1,2,3,',
'author_name' => 'admin',
//Category Parameters
'cat' => 1,
'category_name' => 'blog',
'category__and' => array( 1, 2),
'category__in' => array(1, 2),
'category__not_in' => array( 1, 2 ),
//Type & Status Parameters
'post_type' => 'any',
'post_status' => 'any',
//Choose ^ 'any' or from below, since 'any' cannot be in an array
'post_type' => array(
'post',
'page',
'revision',
'attachment',
'my-post-type',
),
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash'
),
//Order & Orderby Parameters
'order' => 'DESC',
'orderby' => 'date',
'ignore_sticky_posts' => false,
'year' => 2012,
'monthnum' => 1,
'w' => 1,
'day' => 1,
'hour' => 12,
'minute' => 5,
'second' => 30,
//Tag Parameters
'tag' => 'cooking',
'tag_id' => 5,
'tag__and' => array( 1, 2),
'tag__in' => array( 1, 2),
'tag__not_in' => array( 1, 2),
'tag_slug__and' => array( 'red', 'blue'),
'tag_slug__in' => array( 'red', 'blue'),
//Pagination Parameters
'posts_per_page' => 10,
'posts_per_archive_page' => 10,
'nopaging' => false,
'paged' => get_query_var('paged'),
'offset' => 3,
//Custom Field Parameters
'meta_key' => 'key',
'meta_value' => 'value',
'meta_value_num' => 10,
'meta_compare' => '=',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'type' => 'CHAR',
'compare' => '='
),
array(
'key' => 'price',
'value' => array( 1,200 ),
'compare' => 'NOT LIKE'
),
//Taxonomy Parameters
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'color',
'field' => 'slug',
'terms' => array( 'red', 'blue' ),
'include_children' => true,
'operator' => 'IN'
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 1, 2, 3 ),
'include_children' => false,
'operator' => 'NOT IN'
)
),
//Permission Parameters -
'perm' => 'readable',
//Parameters relating to caching
'no_found_rows' => false,
'cache_results' => true,
'update_post_term_cache' => true,
'update_post_meta_cache' => true,
);
// 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>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment