Skip to content

Instantly share code, notes, and snippets.

@danielpataki
Last active September 17, 2018 13:24
Show Gist options
  • Save danielpataki/8b548dd0085c32db64b7 to your computer and use it in GitHub Desktop.
Save danielpataki/8b548dd0085c32db64b7 to your computer and use it in GitHub Desktop.
WP_Query
$args = array(
'tag' => 'featured',
'category__in' => array( 43, 52 ),
'author__in' => array( 255, 930 )
);
$author_featured_posts = new WP_Query( $args );
$args = array(
'number' => 5,
'orderby' => 'count',
'order' => 'DESC',
'fields' => 'ids'
);
$top_categories = get_terms( array( 'category' ), $args );
$args = array(
'category__in' => $top_categories;
);
$top_category_posts = new WP_Query( $args );
$args = array(
'date_query' => array(
array(
'month' => 4,
'day' => 1
),
),
);
// Programmatically placing a gallery shortcode
global $post;
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_status' => 'any',
'fields' => 'ids'
);
$images = new WP_Query( $args );
$image_id_string = implode( ',', $images );
echo do_shortcode( '[gallery ids="' . $image_id_string . '"]' );
$args = array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => '',
'compare' => '!=',
),
),
);
$args = array(
'meta_query' => array(
array(
'key' => 'price',
'value' => '20',
'compare' => '>=',
),
),
'post_type' => 'product',
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order` => 'ASC'
);
$args = array(
'post_type' => 'clue',
'post_password' => 'treasurehunt2015'
);
<?php
$args = array(
'meta_key' => 'rating',
'meta_value' => 4,
'meta_compare' => '>',
'fields' => 'id',
);
$top_users = get_users( $args );
$args = array(
'author__in' => $top_users;
);
$top_author_posts = new WP_Query( $args );
?>
$args = array(
'post_status' => 'future',
'post_type' => 'product',
'category_name' => 'books'
);
global $post;
$args = array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_status' => 'any'
);
// Show posts about ACF (Advanced Custom Fields) from the news category
$args = array(
's' => 'ACF',
'category_name' => 'news'
);
// Search an author's posts
$args = array(
's' => 'custom post types',
'author_name' => 'danielpataki'
);
// Search an array of custom post types
$args = array(
's' => 'wordpress',
'post_type' => array( 'project', 'portfolio' )
);
$args = array(
'post_type' => 'book',
'tax_query' => array(
array(
'taxonomy' => 'mood',
'field' => 'slug',
'terms' => array( 'negative', 'bad', 'sad' ),
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => array( 'comedy', 'romance' ),
'operator' => 'NOT IN',
),
'relation' => 'AND',
),
);
<?php
$args = array(
'author_name' => 'danielpataki',
'category_name' => 'wordpress'
);
$author_posts = new WP_Query( $args );
if ( $author_posts->have_posts() ) :
?>
<?php while ( $author_posts->have_posts() ) : $author_posts->the_post() ?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
<?php the_content() ?>
</div>
<?php endwhile ?>
<?php else : ?>
<h2>Ooops, no posts here!</h2>
<?php endif ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post() ?>
<div <?php post_class() ?>>
<h2><?php the_title() ?></h2>
<?php the_content() ?>
</div>
<?php endwhile ?>
<?php else : ?>
<h2>Ooops, no posts here!</h2>
<?php endif ?>
// Posts from the author with the ID of 42
$args = array(
'author' => 42
);
// All posts except the ones from author 42
$args = array(
'author' => -42
);
// Posts from author 42, 38 and 55
$args = array(
'author' => '42,38,55'
);
// Posts from everyone except author 42 and 38
$args = array(
'author' => '-42,-38'
);
// Posts from author 42, 38 and 55
$args = array(
'author__in' => array( 42, 38, 55 )
);
// Posts from everyone except author 42 and 38
$args = array(
'author__not_in' => array( 42, 38 )
);
$args = array(
'author_name' => 'danielpataki'
);
// Do not cache results
$args = array(
'posts_per_page' => -1,
'cache_results' => false
);
// Don't cache taxonomy data
$args = array(
'posts_per_page' => 10,
'update_post_term_cache' => false
);
// Posts from the category with the ID of 42
$args = array(
'cat' => 42
);
// All posts except the ones from category 42
$args = array(
'cat' => -42
);
// Posts from categories 42, 38 and 55
$args = array(
'cat' => '42,38,55'
);
// Posts from all categories except 42 and 38
$args = array(
'cat' => '-42,-38'
);
// Posts from category 42, 38 and 55
$args = array(
'category__in' => array( 42, 38, 55 )
);
// Posts from all categories except 42 and 38
$args = array(
'category__not_in' => array( 42, 38 )
);
// Posts that are assigned both category 42 and 38
$args = array(
'category__and' => array( 42, 38 )
);
$args = array(
'category_name' => 'book-reviews'
);
$args = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
),
);
// Return all posts from 2014
$args = array(
'date_query' => array(
array(
'year' => 2014,
),
),
);
// Return all posts from January 2015
$args = array(
'date_query' => array(
array(
'year' => 2015,
'month' => 1
),
),
);
// Return all Valentine's Day posts, regardless of year:
$args = array(
'date_query' => array(
array(
'month' => 2
'day' => 14
),
),
);
$args = array(
'post_type' => 'book',
'meta_query' => array(
array(
'key' => 'pages',
'value' => 500,
'compare' => '>',
'type' => 'NUMERIC'
),
),
);
$args = array(
'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' )
);
// Reverse order by post title
$args = array(
'post_type' => 'post',
'orderby' => 'title',
'order' => 'DESC'
);
// List specific posts, preserve given order
$custom_posts = array( 56, 928, 2271, 22, 491, 883 );
$args = array(
'post_type' => 'any',
'post__in' => $custom_posts,
'orderby' => 'post__in',
);
// Order by meta data (grab posts with thumbnails first)
$args = array(
'post_type' => 'post',
'meta_key' => '_thumbnail_id',
'orderby' => 'meta_value',
'order` => 'DESC'
);
$args = array(
'posts_per_page' => 20,
'offset' => 2,
'ignore_sticky_posts' => true
);
// Return only password protected posts
$args = array(
'has_password' => true
);
// Return posts protected with the password: qweasd
$args = array(
'post_password' => 'qweasd'
);
$args = array(
'post_status' => array( 'publish', 'private' ),
'perm' => 'readable',
);
// Return posts with the 'page' post type
$args = array(
'post_type' => 'page'
);
// Return posts from two custom post types
$args = array(
'post_type' => array( 'product', 'post' )
);
// Returns posts from all post types
$args = array(
'post_type' => 'any'
);
// Return draft posts
$args = array(
'post_status' => 'draft'
);
// Return published and scheduled posts
$args = array(
'post_status' => array( 'publish', 'future' )
);
// Returns posts from all statuses
$args = array(
'post_status' => 'any'
);
// Grab post 532 only
$args = array(
'p' => 532
);
// Grab the post with the given slug
$args = array(
'name' => 'guide-to-wp-query'
);
// Retrieve page 55
$args = array(
'page_id' => 55
);
// Retrieve the about page
$args = array(
'pagename' => 'about'
);
// Get the specified 5 posts
$args = array(
'post__in' => array( 31, 36, 39, 91, 119 )
);
// Get all posts except the specified 5
$args = array(
'post__not_in' => array( 31, 36, 39, 91, 119 )
);
// Get all child posts of post 6
$args = array(
'post_parent' => 6
);
// Get posts which are the children of the listed posts
$args = array(
'post_parent__in' => array( 6, 23, 55 )
);
// Get posts which do not have the listed parents
$args = array(
'post_parent__not_in' => array( 6, 23, 55 )
);
$args = array(
's' => 'awesome+wordpress+plugins'
);
// Posts from the tag named "Awesome Colors" or "Awesome People"
$args = array(
'tag_slug__in' => array( 'awesome-colors', 'awesome-people' )
);
// Posts from the contain both "Awesome Colors" and "Awesome People"
$args = array(
'tag_slug__and' => array( 'awesome-colors', 'awesome-people' )
);
// Posts from the tag named "Awesome Colors"
$args = array(
'tag' => 'awesome-colors'
);
// Posts from the tag named "Awesome Colors" or "Awesome People"
$args = array(
'tag' => 'awesome-colors,awesome-people'
);
// Posts from the contain both "Awesome Colors" and "Awesome People"
$args = array(
'tag' => 'awesome-colors+awesome-people'
);
// Posts from the tag with the ID of 23
$args = array(
'tag_id' => 23
);
// Posts from tag 42, 38 and 55
$args = array(
'tag__in' => array( 42, 38, 55 )
);
// Posts from all tags except 42 and 38
$args = array(
'tag__not_in' => array( 42, 38 )
);
// Posts that are assigned both tag 42 and 38
$args = array(
'tag__and' => array( 42, 38 )
);
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => array( 'action', 'comedy' ),
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN',
),
'relation' => 'AND',
),
);
$args = array(
's' => 'awesome+wordpress+plugins',
'posts_per_page' => 8
);
$results = new WP_Query( $args );
echo "A total of " . $results->found_posts . " posts were found.<br />";
echo "We will be displaying " . $results->query_vars['posts_per_page'] . " posts per page if possible.<br />";
echo "We need a total of " . $results->max_num_pages . " pages to display the results".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment