"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

197
votes
6answers
62k 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?
71
votes
3answers
38k 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 ...
17
votes
2answers
12k 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 ...
2
votes
2answers
2k views

Next_posts_link() works only with original $wp_query

I've got custom template that I want to display paged blog posts. This is the beginning of my file: $wp_query = new WP_Query($args); if($wp_query->have_posts()){ ...
4
votes
4answers
10k views

Custom Taxonomy WP_Query for All Terms in a Taxonomy?

Is there an easy way to query for any posts that is tagged with any term from a particular taxonomy? I know this technique: $custom_taxonomy_query = new WP_Query( array( 'taxonomy_name' => ...
7
votes
1answer
11k 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 ...
1
vote
1answer
973 views

Show only oldest post by author

I am working with a heavily customized install of WP. I've set up a specific post type, taxonomy, and roles that gets to use just that post type. Ideally, I'd like to restrict members of that role ...
0
votes
2answers
2k views

Multiple orderby parameters in pre_get_posts() action

Referencing @Otto's response to a question I also had about ordering by multiple fields, here is what he said: Can't do it with a naive WP_Query. Use the posts_orderby filter to add your own ...
2
votes
2answers
3k views

Combining queries with different arguments per post type

I am building a section on a site where I am merging two different post types into one loop, and then displaying them randomly. The problem is, I'm having a hard time finding a way to limit the amount ...
0
votes
1answer
1k views

Custom query with category exclusion and post-meta “whitelist”

I have two widgets, one shows News while the other shows everything except News (We'll call the second one Blog). The blog portion is currently working correctly. The News posts are either ...
3
votes
1answer
1k views

Dynamically link to the latest post or simulate request of specific post in page template

I want to have a link in the wordpress menu pointing to the latest post of a specific category. Because I am not able put a dynamic URL in the wordpress menu, my approach was to put a page with a ...
1
vote
1answer
4k views

Multiple WP_Query loops with Pagination

There are a few other questions about this (and WP_Query pagination seems to be a huge question for a lot of people) so I'm trying to narrow down exactly how to make it function. I'm able to create a ...
2
votes
2answers
3k views

WP_Query and next_posts_link

I cannot figure out how to make next_posts_link() work within my custom WP_Query. Here is the function: function artists() { echo '<div id="artists">'; $args = array( 'post_type' => ...
1
vote
1answer
98 views

Can I specify post__in for one particular post type in the query?

I want to do something like this $query_args = array( 'post_type' => array( 'cpt_1', 'cpt_2' ), 'post__in' => array( 1, 3, 5, 7 ) ); where I can specify which post type the post__in ...
1
vote
1answer
572 views

WP_Query pulling an extra post per page

I'm querying to get a list of all posts, 20 posts_per_page, but the result shows all posts, but with 21 posts per page. If I change posts_per_page to 19, then 20 show. One post is sticky and shows up ...
0
votes
3answers
5k views

Page template query with WP_Query

I'd like to query only pages with a certain page template with WP_Query or a function that would return the post object, but I can't find any information about that on the officiel codex.
1
vote
1answer
555 views

How to paginate attachments in a secondary query as gallery?

I have this code that shows all images attached to my post-type. Is there a way to paginate those? I have like 25 pics and I wan't to avoid scroll as much as I can. Code: <div class="galleries ...
0
votes
3answers
786 views

How to get posts from two categories with WP_Query?

I'm using WP_Query to create 'latest posts' & 'popular posts' sections on my home page. I'm trying to just pull 5 posts from 2 categories (9 & 11) but it will only show posts from cat 9. ...
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
6answers
8k 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 ...
2
votes
2answers
1k views

“pre_get_posts” firing on every query

How can I change arguments for the main query only, and not affect other queries? add_filter('pre_get_posts', 'custom_post_count'); function custom_post_count($query){ ...
2
votes
2answers
3k views

Displaying Posts Related to Other Posts by a Taxonomy Term?

I have a custom post type for musician's profiles, and a taxonomy for their location (categorised hierachically into Countries and then Cities). On the musician's individual profile pages I need to ...
9
votes
5answers
13k 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 ...
5
votes
4answers
15k 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 ...
3
votes
3answers
2k views

Archive Listings Filtered by Date Values in a Custom Field/Post Meta?

(Moderator's Note: The original title was "using archive by date with a custom date") I'm adding an additional date to posts as a custom field. Now I want the archive to show posts by the custom date ...
3
votes
1answer
2k views

WP_Query with checkbox meta_query

I have posted this on StackOverflow too, not sure if that's allowed - if not let me know and I'll remove one (I don't want to look like I'm spamming). Anyway.... I have a custom post type and have ...
1
vote
2answers
2k views

How to order custom post type by multiple custom fields?

How to display posts with 'Event' custom type ordered by 'Start_Hour' and then by 'Start_Minute'? 'Start_Hour' and 'Start_Minute' are numeric custom fields. I tried this, but it doesn't work: $args ...
1
vote
1answer
676 views

Combine two taxonomies in a hierarchical tree

For example, I have WooCommerce products that have taxonomies: brand (e.g. Converse, adidas, D&G) (taxonomy id: product_brand) category (e.g. Boots, Sneakers, Sandals, Heels) (taxonomy id: ...
1
vote
1answer
328 views

How do I create my own nested meta_query using posts_where / posts_join?

Some of my posts (not all) have a price as a meta key/value. Today I use the pre_get_posts action so that my users can search for prices that are between a certain value. This is the code that I'm ...
0
votes
1answer
105 views

assign 2 $args to one wp_query

i need to excute posts from 2 search keywords & put all in one loop to showing by using WP-Query i have trying array_merge but this not worked for me so my last code is: please note: i need to ...
16
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()) : ...
7
votes
4answers
596 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 ...
3
votes
1answer
1k views

Custom Post Type “Event”: chronological list of recurring events

i'm looking for a good and easy way to have a custom post type "event" that can have multiple dates. (I have tested about 20 plugins). I think i have no problem building a metabox allowing the user to ...
8
votes
6answers
11k 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 ...
7
votes
4answers
4k 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, // ...
5
votes
5answers
9k 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 ...
4
votes
2answers
180 views

Order by optional meta key?

I'm building the sorting functionality for a custom post type, and I have a custom meta value for "Featured" posts. This value is set when you tick the checkbox, otherwise it is not set. Using the ...
4
votes
1answer
481 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
2answers
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 ...
3
votes
2answers
2k views

Custom Post Type, WP_Query and 'orderby'

I do have a custom post type with the following setup: $supports = array( 'title' , 'editor' , 'thumbnail' , 'revisions' , 'page-attributes' ); $args = array( 'hierarchical' ...
3
votes
2answers
3k views

Pagination with WP_Query is buggy - working for some pages, but not the others

I'm having a very peculiar bug with pagination :( I'm trying to display 4 posts per page for a category with slug "lastest-news". The pagination function seems to work correctly. There are 33 posts. ...
3
votes
4answers
4k views

Counting Posts of a Given Post Type Having a Specific Taxonomy?

(Moderator's note: The original title was "How to know the number of custom type posts having a specific taxonomy") I'm stuck with the function wp_count_posts()! I created custom post types called ...
4
votes
1answer
1k views

How to paginate wordpress [gallery] shortcode?

I am using wordpress gallery shortcode [gallery] I was wondering if there is anyway to paginate them without using a plugin.
1
vote
1answer
972 views

How to sort CPT by custom meta value (date), and return posts month by month

For future reference: I am using the great Meta Box Plugin to help expedite meta box creation for my custom post types. My goal: To build a highly customizable events section closely resembling a ...
-1
votes
1answer
273 views

Integrating custom API for post content into Admin interface & Public Website [closed]

EDITED question to be more specific and added bounty in hopes someone has a solid coded solution: So, here is an interesting task I have been trying to find a solution for. I have added a bounty ...
4
votes
2answers
4k 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
4answers
5k views

How do I exclude a custom taxonomy from the post loop

Is there a simple or easy way to exclude all posts from a custom taxonomy in the loop? I've been looking high and low, and neither SE, SO or Google seem to have a straight answer. I know it can be ...
2
votes
2answers
721 views

Sorting: custom query with orderby meta_value_num THEN by title

i try to run a custom post type query to match following criteria: sort movies first by year in descending order, after that ("inside" the year order) by title alphabetically. desired output: ...
2
votes
2answers
327 views

Orderby = none not working [duplicate]

I'm trying to set 'orderby = none' to my loop but it is not working. Here's my code: $query = new WP_Query(array('showposts'=>2, 'post__in' => array(99,4,5,2,8,55), 'orderby'=>'none')); ...
1
vote
1answer
50 views

Query Posts but only return 1 from a custom taxonomy?

I want to query my posts and only return one post from a custom taxonomy. Is this possible? Here is an example: I have a custom taxonomy called Author. I have 200 posts with various authors. I want ...