"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.
148
votes
3answers
39k 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?
49
votes
3answers
18k 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 ...
14
votes
3answers
532 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 ...
14
votes
2answers
7k 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 ...
13
votes
3answers
5k 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()) : ...
11
votes
2answers
1k 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
5answers
3k 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 ...
10
votes
3answers
272 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 ...
9
votes
5answers
4k 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 ...
8
votes
3answers
7k 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 ...
8
votes
3answers
87 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 ...
7
votes
4answers
8k 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 ...
7
votes
3answers
3k 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, // ...
7
votes
1answer
4k 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 ...
6
votes
2answers
7k 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',
...
6
votes
1answer
7k 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 ...
6
votes
1answer
3k 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 ...
6
votes
5answers
5k 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 ...
6
votes
2answers
292 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 ...
6
votes
1answer
242 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 ...
6
votes
1answer
953 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
1answer
742 views
WPML with WP_Query serving up all 3 languages
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
2answers
793 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
4k 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 ...
5
votes
1answer
62 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
2answers
752 views
How to set a custom post type to have viewable future posts
I've setup a CPT to act the same ways as posts but used to post event details.
Thing is that some of the posts are in the future and such have a future date set on them. Problem is that normal users ...
4
votes
1answer
4k 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' => ...
4
votes
4answers
7k 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 ...
4
votes
2answers
1k views
Get posts by menu ID
I'm a little stuck here. I have a menu in Wordpress with some posts in it.
What I would like is to retrieve all the posts as objects.
I think I'm pretty close, but the SQL query doesn't seem to work ...
4
votes
1answer
347 views
How-to exclude terms from the main query the most performant way?
This Q is a follow up to this answer on the Q: "How to exclude a specific term for the search?".
4 ways to filter out posts that have a specific term
Type | Pro | ...
4
votes
1answer
403 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', ...
4
votes
1answer
3k views
Custom Taxonomy not working with posts_per_page in new WP_query (pagination problem)
OK, so i'm messing with Custom Taxonomies for the second time and in both instances i've run into this problem where using the posts_per_page argument in either a query_posts or WP_query scenario.. ...
4
votes
1answer
2k views
Search multiple custom fields by using meta_query
I'm trying to create a search results listing for a custom post type and the problem I'm running in to is with the way the 'meta_query' parameter is stringing the passed values together. It appears ...
4
votes
1answer
204 views
Changing the meta_query of the main query based on custom query_vars and using pre_get_posts
I've registered custom rewrite rules and query_vars to use for displaying a list of events based on ISO date format. For example when a user requests the URL, http://site.com/by-date/2013-04-04/, my ...
4
votes
2answers
298 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 ...
4
votes
3answers
7k views
Filter by one custom field, order by another?
I have a custom post type "Listing" and I want to get all Listings that have a custom field gateway_value != 'Yes', and order the results by another custom field, location_level1_value. I can get the ...
4
votes
1answer
124 views
How can I save an array from a random post sequence for later use?
I'm building a random workout generator on Wordpress to help with my fitness, and am stuck at saving random post arrays for later reference.
I'll explain.
The generator should work in two main ...
4
votes
2answers
3k views
query_posts() in function makes global $wp_query out of sync?
This has me stumped for a while already. Either I am missing something very obvious or something very not obvious. I am also not entirely sure if this has something to do with WP or purely PHP ...
4
votes
1answer
77 views
Tracing the life of a query
I can't find documentation regarding this - but is there a way to see how a query might bounce around from one filter to another? I've got a list of possible wp_filters and their relevance but I ...
4
votes
1answer
568 views
Register custom query args parameter for WP_Query()
I have written a plugin which can store a table of technical specifications for each post (product) in an own database table.
For the search function of the site I want to add a custom parameter to ...
4
votes
1answer
610 views
next_posts_link not working for loop called with ajax
On my homepage I display a loop of posts that I allow to be replaced by ajax search results and other custom loops. Those results are returned in a template with a custom wp_query. The issue is ...
4
votes
1answer
193 views
Query causing load because of SQL_CALC_FOUND_ROWS post counting?
I found this query causing some load in the server, my hosting provdier asked me to look on it.
SELECT SQL_CALC_FOUND_ROWS wp_posts
I think this select no. rows found in wp_posts table.. but i ...
4
votes
1answer
388 views
Why does `get_permalink()` produces an add. DB request without $post->filter?
I'm currently working on a custom loop/query where I need to add the permalink of a post to the array of posts I'm retrieving from the DB.
Sadly get_permalink() produces one additional DB request ...
4
votes
2answers
431 views
Build a content and excerpt grid loop with paging and options for # of posts
What I'd like to do: on index.php, a loop that shows a selectable number of full posts using the_content and then below that shows a selectable number (the number of posts can be hardcoded in the ...
4
votes
1answer
625 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 ...
4
votes
4answers
4k 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 ...
4
votes
1answer
180 views
find a random blogid across my multisite network that has at least one post published
I need to find a random blog across my multisite network that has at least one post published of a certain post_type, and return its blogid as a variable so I can use it in a switch_to_blog() call.
I ...
4
votes
0answers
116 views
Custom query incorrectly returning everything [closed]
I'm missing something somewhere, but I cannot connect the dots. Everything outputs properly except that the query returns everything from the DB, not just posts. Can anyone spot a reason for this ...
3
votes
5answers
5k 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 ...
3
votes
2answers
262 views
Why does pagination always break when used on any form of a home page?
Why does WP pagination break when used on the homepage?
If you take the code below and use it in a page template it works perfectly (make sure you have 3 or more posts). However, as soon as you use ...