124 votes

Get post ids from WP_Query?

Use the fields argument in your query. fields (string) - Which fields to return. All fields are returned by default. There are two other options: - 'ids' - Return an array of post IDs. - '...
user avatar
  • 63.7k
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
56 votes
Accepted

Get post ids from WP_Query?

Try $post_ids = wp_list_pluck( $latest->posts, 'ID' ); Read wp_list_pluck
user avatar
  • 4,900
48 votes

How can i get count from query post

The accepted answer here is wrong which is also confirmed in my situation. Please, compare from the referenced page: $post_count The number of posts being displayed. $found_posts The total number of ...
user avatar
35 votes
Accepted

Some doubts about how the main query and the custom query works in this custom theme?

Your actual question is basically when to run a custom query and when to make use of the main query. Lets break it down in three parts PART ONE When to run a custom query (This is not a definitive ...
user avatar
31 votes

Custom query with orderby meta_value of custom field

This issue in general is cleared up as of WordPress 4.2 by using named queries. e.g. $args = array( 'post_type' => 'services', 'orderby' => 'order_clause', 'meta_query' => array( ...
user avatar
29 votes
Accepted

Nested meta_query with multiple relation keys

The question was for Wordpress 3.0, but just in case someone has the same question for a more recent version, from Wordpress Codex: "Starting with version 4.1, meta_query clauses can be nested in ...
user avatar
  • 1,426
27 votes
Accepted

Display posts of the last 7 days

In addition to birgire's solution, as of WordPress 3.7, you can use Date parameters. Your arguments would look like this to filter posts from the last 7 days: $args = array( 'post_type' => '...
user avatar
  • 8,609
20 votes

How do I query a custom post type with a custom taxonomy?

i am using this query to fetch custom posts (FAQs Posts) with its custom taxonomy (faq_category). since {taxonomy} parameter in WP_Query args was deprecated since v.3.1 and introduced {tax_query}. ...
user avatar
19 votes
Accepted

How do I exclude a custom taxonomy from the post loop

You would want to use the NOT EXISTS operator along with passing the taxonomy slug, which tells the query not to include any of a chosen category from your custom taxonomy inside the loop. To exclude ...
user avatar
18 votes
Accepted

WP Query where title begins with a specific letter

It's not possible with WP_Query directly out of the box, but by using the posts_where filter to look for a custom argument on WP_Query, it is possible to add this functionality. Assuming starts_with ...
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
14 votes
Accepted

Why query_posts() isn't marked as deprecated?

Essential question Let's dig into the trio: ::query_posts, ::get_posts and class WP_Query to understand ::query_posts better. The cornerstone for getting the data in WordPress is the WP_Query class. ...
user avatar
  • 4,169
13 votes

Nested meta_query with multiple relation keys

Meanwhile this is possible, see documentation with example and explanation: Old link: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters Update: 2021, new link: https://...
user avatar
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
10 votes

How can i get count from query post

To get the total number of posts WP_Query returns use "found_posts" Here is the example - <?php $args = array( 'post_type' => 'post' ); ...
user avatar
10 votes
Accepted

WP_query parameters for date range

Copied from StackOverflow: WP_Query offers a date_query parameter, allowing you to set a range with before and after. https://developer.wordpress.org/reference/classes/wp_query/#date-parameters $...
user avatar
  • 2,893
10 votes

Why query_posts() isn't marked as deprecated?

I have just created a new trac ticket, ticket #36874, to propose the deprecation of query_posts(). Whether or not it will be accepted remains a good question. The real big issue with query_posts() ...
user avatar
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

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
8 votes

How to Get All Posts with any post status?

In most cases you can use get_posts() with 'any' parameter for this: $posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', 'post_type' => 'my-post-type', ) ); But ...
user avatar
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
Accepted

Display Custom Post Type Fields

Custom fields are saved in the post_meta table. In your query you got the post title and post ID, so now you have to get post meta. Use: <?php get_post_meta( get_the_ID(), '_location', true ); ?&...
user avatar
  • 396
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

How to ensure that is_search() return false after query_posts

Your real big problem here is the use of query_posts. It breaks the main query object, and sets the main query object to the query made by query_posts. What you are seeing is quite normal. Your real ...
user avatar
7 votes

Is `query_posts` really slower than secondary query?

You ask: is query_posts really slower than some secondary query... The fact is that if you're calling query_posts() from a theme then it already is a secondary query. WordPress has already queried ...
user avatar
  • 19.7k
7 votes
Accepted

posts_per_page => 1 shows 2 posts

Most probably you have a sticky post there. If you want exact number of posts you need to ignore sticky posts. So your query should look like this: query_posts( array( 'orderby' => 'rand', ...
user avatar
6 votes

How to Display Post View Count

Add this to single.php, and make sure you paste in inside the loop. <?php setPostViews(get_the_ID()); ?> Lastly, to display the number of views a post has, just add this where you want it to ...
user avatar
  • 227
6 votes
Accepted

ascending order custom post type

You don't declare the order of the results when you register the post type. Instead, you do at the time the results are requested. If you're using WP_Query to get the results then you add your ...
user avatar
6 votes
Accepted

Query Posts by Custom Field

You can achive this using WP Query with meta_query argument as following. $args = array ( 'post_type' => array( 'post' ), // YOUR POST TYPE 'meta_query' => ...
user avatar

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