"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 default object that holds the main query object on each WordPress page.

learn more… | top users | synonyms

204
votes
6answers
69k 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. What's the deal?
73
votes
3answers
45k 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 ...
19
votes
4answers
14k 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 ...
17
votes
4answers
8k views

Is it necessary to use wp_reset_query() in a WP_Query call?

I'm using the following code to retrieve posts: <?php $featuredPosts = new WP_Query(); $featuredPosts->query('showposts=5&cat=3'); while ($featuredPosts->have_posts()) : ...
16
votes
3answers
2k views

Should I use Pre Get Posts or WP_Query

I have the following query which I call in my taxonomy.php template via query_brands_geo('dealers', 'publish', '1', $taxtype, $geo, $brands); This function works perfectly. However after reading ...
13
votes
5answers
7k views

How to extend WP_Query to include custom table in query?

I have been days over this issue now. Initially it was, how to store a user's follower data in database, for which I got couple of nice recommendations here at WordPress Answers. After, following the ...
12
votes
6answers
10k 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 ...
12
votes
1answer
10k views

numberposts? showposts? posts_per_page?

So the documentation on the Codex is pretty clear that showposts is deprecated. But that same documentation mentions nothing of numberposts. Instead, posts_per_page is listed. But if we turn to the ...
11
votes
2answers
19k 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', ...
11
votes
2answers
2k views

Using pre_get_posts with WP_Query

I was reading Stephen Harris's excellent answer to this question regarding the use of WP_query(), query_posts() and pre_get_posts. He says "pre_get_posts is a filter, for altering any query. It is ...
11
votes
3answers
18k views

How to Get All Posts with any post status?

i am creating a frontend dashboard where i need to show all the post by the current user. So i need to show posts with all post status. Mainly published, trashed and the pending. I am now using ...
11
votes
3answers
504 views

What is appropriate flow for custom data from URL in WP_Query?

I have multiple stage process that integrates complex faceting into WP queries. Problem is - the deeper I get the more fuzzy I become on how it is supposed to work and I could use a guideline rather ...
10
votes
2answers
12k 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 ...
9
votes
5answers
15k views

How can I show posts only if meta_value 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 ...
9
votes
6answers
13k views

Wp get all the sub pages of the parent using wp query

Here is my code $my_wp_query = new WP_Query(); $all_wp_pages = $my_wp_query->query(array('post_type' => 'page','post_parent'=>$parid,'orderby'=>'title','order'=>'ASC' )); It displays ...
9
votes
1answer
569 views

Why is the loop not empty on some 404s?

I came across a weird issue. Say you access a random url, three or more levels deep: http://example.com/a/b/c http://example.com/a/b/c/d ... Then is_404() is true. So far so good. But for some ...
9
votes
3answers
209 views

Posts with at least 3 tags of a list of tags

There is a set of tags against which posts that have at least 3 of those tags should be matched. Say there is the tags {foo, bar, chocolate, mango, hammock, leaf} I would like to find all posts with ...
8
votes
1answer
301 views

Mixing regular and custom post types (with meta_query) on home page

Not sure how to achieve this. I'm trying to mix standard posts and custom posts on a site homepage but I only want to display the custom posts if a meta value is set. Displaying the posts works fine ...
7
votes
4answers
1k views

How to merge two queries together

I am trying to order the posts in a category by showing the posts with images first and then posts without images last. I have managed to do that by running two queries and now I want to merge the two ...
7
votes
1answer
12k views

Passing and retrieving query vars in wordpress

I have two authors pages, one displays about 5 posts. Then I'm trying to setup another page that will be all of their posts. I have created a template called moreauthorposts.php and I'm trying to ...
7
votes
1answer
6k 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 ...
7
votes
3answers
199 views

Can I force WP_Query to return no results?

I'm working on a website with a search feature that allows users to search through a lot of post meta. There is a specific search pattern that I would like to forcibly return no results for. The ...
7
votes
1answer
9k 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 ...
7
votes
2answers
1k views

What is “main query”? [duplicate]

Possible Duplicate: How to know which one is the main query? I'm curious to know what is the so called "main query"? What I have is two queries on front page. if (have_posts()) : while ...
7
votes
1answer
3k views

Multiple relationship for multiple tax_query in WP_Query

I want to use the WP_Query class to filter some of my posts. The problem I facing now is handling the taxonomy query. Normally, the wp_query only handle one relationship for tax_query (either AND or ...
7
votes
4answers
5k views

Broken? WP_Query and “attachment” as a post type

I have a gallery attached to a page. On that page, I'm running the following query: $events_gallery = new WP_Query( // Start a new query for our videos array( 'post_parent' => $post->ID, // ...
6
votes
1answer
15k 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 arguements: $args = array( 'post_status' => 'publish', 'post_type' => ...
6
votes
2answers
244 views

Is it possible to query all posts that don't have an attachment?

I want to get a list of all posts that don't have an attachment and delete them. This question takes about getting all posts that have an attachment, but I want the reverse of it. The brute force ...
6
votes
1answer
88 views

Should we trust the post globals

@toscho left a comment to this answer which had me thinking again. How much trust should he have in the global scope, specially regarding the post globals like $post. So what? The global variable ...
6
votes
1answer
168 views

Inject post (from specific category) between posts in Loop

I would like to add a post (from specific category) between posts in the WordPress Loop (such as 'Sponsors'). Example: P P P S P P S P P S P P How can I achieve this? I am a beginner with coding, so ...
6
votes
1answer
6k views

Custom Taxonomy and Tax_Query

I've been having a lot of trouble getting a WP_Query running with a tax_query on my custom taxonomy. I'm 99.9% sure that my register_taxonomy is correct, as I'm able to tag posts with the right term, ...
6
votes
4answers
753 views

Best way to detect if you are in a SINGLE POST page

So this may seem like a pretty menial thing, but follow me here. I'm trying to add some logic through the pre_get_posts action. This is the entire WP_Query object given to me. (see the end) Things I ...
6
votes
3answers
5k views

Getting Post Comments for post ID using WP_Query() and a Custom Loop?

(Moderator's note: The original title was "Is there a way to get post comments by post ID on a custom loop using WP-Query()?") Hi! I'm running a custom loop using WP_Query, that only displays one ...
6
votes
1answer
1k views

Using Wordpress to make a “Product Search” type navigation drilldown

This is somewhat of a follow-up to a question I asked some time ago: Trying to design nice category drilldown with one "held" taxonomy and a repeatable list of categories I've sort of got ...
6
votes
4answers
6k views

Querying posts from multiple sites in a network?

I realize there have been a few questions which dance around the solution I am after but I believe I am looking for something specific. This is actually a two part question: 1) My goal is to have ...
6
votes
1answer
2k views

WPML with WP_Query serving up all 3 languages [on hold]

I've got something about this on the WPML support forums, but I figured I could ask here as well. Here's the important info: I'm running a WP Multisite setup with custom post types, custom fields, ...
5
votes
5answers
10k views

Display all posts in a custom post type, grouped by a custom taxonomy

I’m working on a member page where I use a custom post type with a custom taxonomy. My custom post type is called “member” and my custom taxonomy is called “member_groups”. I want to list all the ...
5
votes
3answers
2k views

How to use a custom post type archive as front page?

I'd like to use a custom post type archive as a site's front page, so that http://the_site.com/ is a custom post type archive displayed according to my archive-{post-type}.php file. Ideally I ...
5
votes
4answers
18k views

query_post by title?

is it possible to create a loop of posts using WP_Query or query_posts using the title? ie $args = array('post_title'='LIKE '.$str.'% '); $res = WP_Query($arg); // the loop... // trying this ...
5
votes
2answers
964 views

WP_Query leaking absurd amounts of memory

Every time I call WP_Query() in the function below, Wordpress leaks 8 megs of memory. And since I call this function a lot, things get hairy pretty quickly... :( I've tried unsetting the resulting ...
5
votes
2answers
156 views

How to force a query conditional?

There is a long-standing WordPress core bug (#16373) in which, if custom query variables are registered and present in the query string, the query will not set is_front_page() true, even if 'page' == ...
5
votes
1answer
772 views

how to show posts that are missing a meta_value

How can I build a query to find posts that DO NOT contain a certain meta key or meta value? for example: query_posts( array( 'meta_query' => array( array( 'key' => 'feature', ...
5
votes
2answers
1k views

Usage of the new “posts_clauses” filter in Wordpress 3.1?

Just noticed that 3.1 added a new filter to customize queries: posts_clauses. All I could find about it is that instead of using the separate query filters like posts_where or posts_join, you can ...
5
votes
2answers
945 views

Differences Between WP_Query() and get_posts() for Querying Posts?

What are the differences between using WP_Query() and get_posts()? Which is better to use in what case and why?
5
votes
1answer
164 views

How to Compare Two Meta Fields

Consider two meta fields balance and monthly_price. How you would get all posts that has balance < monthly_price Thinking of creating meta query but our both fields are arbitrary or do not have ...
5
votes
1answer
4k views

How do I use WP_query with multiple post IDs?

I want to query multiple posts with an array of IDs (note: I am querying a custom post type). Here's what I have, which isn't working: $myarray = array('144', '246'); $args = array( 'post_type' ...
5
votes
3answers
2k views

Query Posts by Custom Field 'Price'

I'd like to display all posts within a given price range. For example: When a user inputs 100 and 1000 (in two separate form fields) - my site will display all posts that have a custom field called ...
5
votes
1answer
78 views

IP address character limit

my client in his template is using <?php comment_author_IP(); ?> to display IP's of comment authors. Recently he came to me with idea of limiting whole number to several characters. Final result ...
5
votes
1answer
2k views

AJAX search on post pages by custom post type

I'm having an issue creating an ajax search on my single post pages. I need to limit the search results to the custom post types "fod_videos" and "post" and category 12. My issue is the search is ...
5
votes
1answer
215 views

Should I use Transients with W3 Total Cache APC Caching? [closed]

Should I use Transients in my queries like this? <?php if ( ! $my_paged = absint( get_query_var( 'paged' ) ) ) $my_paged = 1; if ( ! $my_query = get_transient( "567recent_$my_paged" ) ) { ...