Last active
September 17, 2018 13:24
-
-
Save danielpataki/8b548dd0085c32db64b7 to your computer and use it in GitHub Desktop.
WP_Query
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
$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 ); |
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
$args = array( | |
'date_query' => array( | |
array( | |
'month' => 4, | |
'day' => 1 | |
), | |
), | |
); |
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
// 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 . '"]' ); |
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
$args = array( | |
'meta_query' => array( | |
array( | |
'key' => '_thumbnail_id', | |
'value' => '', | |
'compare' => '!=', | |
), | |
), | |
); |
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
$args = array( | |
'meta_query' => array( | |
array( | |
'key' => 'price', | |
'value' => '20', | |
'compare' => '>=', | |
), | |
), | |
'post_type' => 'product', | |
'meta_key' => 'price', | |
'orderby' => 'meta_value_num', | |
'order` => 'ASC' | |
); |
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
$args = array( | |
'post_type' => 'clue', | |
'post_password' => 'treasurehunt2015' | |
); |
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
$args = array( | |
'post_status' => 'future', | |
'post_type' => 'product', | |
'category_name' => 'books' | |
); |
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
global $post; | |
$args = array( | |
'post_parent' => $post->ID, | |
'post_type' => 'attachment', | |
'post_status' => 'any' | |
); |
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 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' ) | |
); | |
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
$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', | |
), | |
); |
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 | |
$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 ?> |
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 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 ?> |
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
// 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 | |
); |
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
// 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' | |
); |
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
// 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 ) | |
); |
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
$args = array( | |
'category_name' => 'book-reviews' | |
); |
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
$args = array( | |
'date_query' => array( | |
array( | |
'after' => 'January 1st, 2013', | |
'before' => array( | |
'year' => 2013, | |
'month' => 2, | |
'day' => 28, | |
), | |
'inclusive' => true, | |
), | |
), | |
); |
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
// 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 | |
), | |
), | |
); | |
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
$args = array( | |
'post_type' => 'book', | |
'meta_query' => array( | |
array( | |
'key' => 'pages', | |
'value' => 500, | |
'compare' => '>', | |
'type' => 'NUMERIC' | |
), | |
), | |
); |
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
$args = array( | |
'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' ) | |
); |
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
// 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' | |
); | |
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
$args = array( | |
'posts_per_page' => 20, | |
'offset' => 2, | |
'ignore_sticky_posts' => true | |
); |
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
// Return only password protected posts | |
$args = array( | |
'has_password' => true | |
); | |
// Return posts protected with the password: qweasd | |
$args = array( | |
'post_password' => 'qweasd' | |
); | |
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
$args = array( | |
'post_status' => array( 'publish', 'private' ), | |
'perm' => 'readable', | |
); |
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
// 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' | |
); | |
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
// 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' | |
); | |
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
// 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 ) | |
); | |
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
$args = array( | |
's' => 'awesome+wordpress+plugins' | |
); |
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
// 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' ) | |
); |
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
$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', | |
), | |
); |
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
$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