Questions tagged [wp-query]
"wp-query" ambiguously refers to one of two things. 1) WP_Query is a class native to WordPress which allows users to query the posts database using a number of criteria. 2) $wp_query is the global variable that holds the main query object prepared by WordPress based on a given request.
5,753
questions
463
votes
7
answers
194k
views
When should you use WP_Query vs query_posts() vs get_posts()?
It seems like half the tutorials in the Codex and around the blogosphere use query_posts() and half use WP_Query.
They all do similar things, so when should I use one over the others?
179
votes
5
answers
200k
views
When to use WP_query(), query_posts() and pre_get_posts
I read @nacin's You don't know Query yesterday and was sent down a bit of a querying rabbit hole. Before yesterday, I was (wrongly) using query_posts() for all my querying needs. Now I'm a little bit ...
78
votes
3
answers
196k
views
WP_Query - Order results by meta value
I've checked around and haven't seen an answer which works as of yet. I have a WP_Query with the following arguments:
$args = array(
'post_status' => 'publish',
'post_type' => 'listing',...
69
votes
3
answers
106k
views
Query all posts where a meta key does not exist
I am trying to get a query to retrieve all the posts where a specific meta_key does not exist and then create it.
I am having problems finding those posts as the query I am testing does not seem to ...
63
votes
2
answers
178k
views
Meta_query compare operator explanation
I noticed that there are bunch of operator can be use for compare in meta_query.
However, I am not quite sure what operator I should use, it is somehow confusing like = and LIKE operator.
I would ...
62
votes
6
answers
204k
views
How to Get All Posts with any post status?
I am creating a front end dashboard where I need to show all the posts by the current user. So, I need to show posts in all states, mainly published, trashed and the pending. I am now using a simple ...
62
votes
13
answers
89k
views
Using wp_query is it possible to orderby taxonomy?
My question is simple, I'm using WP_Query to retrieve some custom type posts filtering by a taxonomy using tax_query.
Now my problem is I would like to orderby the taxonomy, but from documentation ...
62
votes
1
answer
172k
views
WP_Query by just the id?
In the codex for WP_Query I see you can query by page_id=7 for pages or by p=7 for posts. Is there a way to get a post of any post type by ID? Like id=7 that will get it no matter if it's a page, post ...
60
votes
3
answers
209k
views
How to get an array of post data from wp_query result?
When run a query with WP_Query method, I got an object. I understand that I can then do the loop to display stuffs. But, my goal is not to display anything, instead, I want to get some post data by ...
55
votes
6
answers
185k
views
WP_Query with "post_title LIKE 'something%'"?
I need to do a WP_Query with a LIKE on the post_title.
I started with this regular WP_Query:
$wp_query = new WP_Query(
array (
'post_type' => 'wp_exposants',
'...
55
votes
4
answers
163k
views
wp query to get child pages of current page
Can anyone please help me to with the wp_query.
I am making a template file/loop to create and archive page of the current page children pages.
This query needs to be automatic as I am using in on ...
51
votes
10
answers
114k
views
How to only display posts whose meta_value field is not empty?
Three people have already tried to solve this, and we're coming up nil. I want to show only posts that have a value in the meta_key 'featured_image'.
So... if 'featured_image' is not empty, show the ...
51
votes
14
answers
81k
views
meta_query with meta values as serialize arrays
I'm working on a project in which I'm creating a custom post type and custom data entered via meta boxes associated with my custom post type. For whatever reason I decided to code the meta boxes in ...
49
votes
2
answers
89k
views
How to get post id of static front page?
I'm trying to use the front page as a default of sorts for featured images (if no featured image is set, I want to use the front page's, for example)
But I'm having trouble finding out how to get the ...
48
votes
5
answers
92k
views
Get post ids from WP_Query?
Is there a way I can retrieve an array of post ids queried from the following:
$latest = new WP_Query( array (
'orderby' => 'rand',
'posts_per_page' => 3
));
if ( $...