88 votes

When should you use WP_Query vs query_posts() vs get_posts()?

query_posts - You should never ever use query_posts. Apart from what @Rarst has said, the really big issue with query_posts is, it breaks the main query object ( stored in $wp_query ). A lot of ...
user avatar
72 votes
Accepted

Get Posts Under Custom Taxonomy

Your tax query is incorrect, field should be the field you want to query on: term_id, name, or slug - $posts_array = get_posts( array( 'posts_per_page' => -1, 'post_type' => ...
user avatar
  • 76.3k
19 votes
Accepted

Wordpress get_posts by category

In all probability you are using a custom taxonomy, and not the build-in category taxonomy. If this is the case, then the category parameters won't work. You will need a tax_query to query posts from ...
user avatar
16 votes
Accepted

How to get post content by calling ajax?

First, you should always use the WordPress AJAX methods, not a custom function for that. See AJAX in Plugins in the Codex. With that practice in mind, you can set up your request like this. Change ...
user avatar
  • 7,409
12 votes
Accepted

Query posts ordering by title, but ignore " and special characters

Try this... $posts = get_posts( array( "orderby"=> "slug", "order" => "ASC", "post_type" => "my-custom-post-type", "posts_per_page" => -1, "...
user avatar
  • 3,621
11 votes

When to use WP_query(), query_posts() and pre_get_posts

I modify WordPress query from functions.php: //unfortunately, "IS_PAGE" condition doesn't work in pre_get_posts (it's WORDPRESS behaviour) //so you can use `add_filter('posts_where', ....);` OR ...
user avatar
  • 5,321
11 votes
Accepted

Why favour the standard WP loop over iterating over (new WP_Query())->get_posts()?

Several reasons 1. Filters and Actions By using the standard loop, you execute various filters and actions that plugins rely on. Additionally, you set up the_post correctly, allowing functions such ...
user avatar
  • 53.3k
10 votes
Accepted

get_posts with multiple categories

As documented in the codex you could use the following $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) ); //post has to be in category with ID 2 AND 6
user avatar
  • 352
10 votes
Accepted

How to order by post_status?

You can use 'posts_orderby' filter to change the SQL performed. Note that: using get_posts() you need to set 'suppress_filters' argument of false for the filter to be performed if you don't ...
user avatar
  • 44.8k
10 votes
Accepted

How to find out what "Blog pages show at most" is set to

You can get that value with get_option('posts_per_page');
user avatar
  • 3,227
9 votes

setup_postdata() does not seem to be working?

global post; does not work with setup_postdata($post); if you want to use the the_title() family of commands etc. It's in https://codex.wordpress.org/Function_Reference/setup_postdata Instead use //...
user avatar
9 votes

When should you use WP_Query vs query_posts() vs get_posts()?

query_posts(): might be used in one and only case if you need to modify main query . It sets a lot of global variables; get_posts(): it is very similar in mechanics and accepts same arguments, but ...
user avatar
  • 159
9 votes
Accepted

How to use the_posts_navigation for wp_query and get_posts?

the_posts_navigation() is simply a wrapper function for get_the_posts_navigation() which issimply a wrapper function for paginate_links. The first two functions uses the the same exact parameters that ...
user avatar
9 votes
Accepted

Using get_posts vs. WP_Query

This is directly related to, and a consequence of WordPress.com VIP At VIP, we deal with sites that range in the hundreds of millions of page views per week. As a result, situations that can slow ...
user avatar
  • 53.3k
8 votes

get_posts - get all posts by author id

global $current_user; $args = array( 'author' => $current_user->ID, 'orderby' => 'post_date', 'order' => 'ASC', 'posts_per_page' =>...
user avatar
  • 201
8 votes

When to use WP_query(), query_posts() and pre_get_posts

Just to outline some improvements to the accepted answer since WordPress evolved over the time and some things are different now (five years later): pre_get_posts is a filter, for altering any ...
user avatar
  • 4,169
8 votes

Get all pages and posts with get_pages() or get_posts()

Why not use WP_Query instead. Like this. $args = array( 'post__not_in' => $inclPages, 'post_type' => array( 'page', 'project' ) 'orderby' => 'menu_order' ); $pages = new ...
user avatar
  • 8,181
8 votes
Accepted

Cache Get_posts

You can use transient cache to cache your custom query. Here is a simple code to set_transient cache for your query for 12 hours. In 12 hours WordPress will not make new query but fetch posts from ...
user avatar
  • 8,181
8 votes
Accepted

WP Query post meta value

Thanks to @Howdy_McGee. He gave me the hint to look after serialized meta queries. With this code I get the desired result. $args = array( 'posts_per_page' => -1, 'orderby' =>...
user avatar
  • 352
8 votes

How to use the_posts_navigation for wp_query and get_posts?

I have a custom template and I struggled hours to show the pagination component. here what's worked for me. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( '...
user avatar
  • 181
7 votes

WordPress get posts by date without query_posts

As of 3.7 you can use date_query http://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters So it would look like: $args = array( 'posts_per_page' => 5, 'post_type' => '...
user avatar
  • 213
7 votes

Wordpress get_posts by category

<ul> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $...
user avatar
7 votes
Accepted

get_template_part in for loop

Your problem is that the variable passed to setup_postdata() must be the global $post variable, like this: // Reference global $post variable. global $post; // Get posts. $posts = get_posts(array( ...
user avatar
6 votes

get_post random and order by not working

If you're hosted on WP Engine, you need to manually enable random ordering in your settings or else 'orderby' => 'rand' won't work. https://wpexplorer-themes.com/total/docs/random-order-wpengine/
user avatar
  • 278
6 votes

How do I get posts by multiple post ID's?

If you can't get the above to work, make sure you add post_type: $args = array( 'post_type' => 'pt_case_study', 'post__in' => array(2417, 2112, 784) ); $posts = get_posts($args);
user avatar
6 votes
Accepted

Get all pages and posts with get_pages() or get_posts()

get_pages does not work with posts or post types that is not hierarchical like pages, so if your post type is hierarchical like posts, it would not work This function can also retrieve other post ...
user avatar
6 votes

How to limit get_posts()?

You can use posts_per_page or numberposts. http://codex.wordpress.org/Template_Tags/get_posts#Parameters EDIT: Regarding your edit. You cannot have the posts_per_page different to the LIMIT in the ...
user avatar
  • 128
6 votes

Get Posts Under Custom Taxonomy

You should use get_terms() for taxonomies when you can. <?php /* Add your taxonomy. */ $taxonomies = array( 'fabric_building_types', ); $args = array( 'orderby' => 'name', ...
user avatar
6 votes
Accepted

date_query not returning some posts in date range

Your syntax is incorrect for your date_query. It should be an array of an array, not just an array. I also suspect that your problem might be related to PHP and not Wordpress. Your before date is ...
user avatar
6 votes
Accepted

Delete all posts from WordPress except latest X posts

The offset parameter is ignored with posts_per_page set to -1 in WP_Query. If you look at the source code in the WP_Query class, posts_per_page=-1 sets nopaging to true. if ( !isset($q['nopaging']) ) ...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible