Refers to a hook in WordPress Core that can be used to alter the results returned by a WP_Query based database query.

learn more… | top users | synonyms

72
votes
3answers
42k 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 ...
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 ...
6
votes
2answers
6k views

Theres a way to use $query->set('tax_query' in pre_get_posts filter?

Theres a way to use $query->set('tax_query' in pre_get_posts filter? for example next code is not altering the query. Note that im building $taxonomies from and custom search. function ...
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 ...
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 ...
2
votes
2answers
2k views

Include and Exclude Taxonomies From Archives & Feeds Using 'pre_get_posts'

What I am trying to do? My blog uses a custom taxonomy called edition with terms like us-canada (6), eu (7) and india (8) -- term slug (ID). I want to make sure that posts not assigned to any ...
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){ ...
1
vote
2answers
1k views

Sort custom-posts in archive.php via meta-key?

this question is strongly related to this question where I already got a working answer. I have a custom-post-template named "wr_event" and a custom-taxonomy named "event_type". This "event-posts" ...
0
votes
1answer
967 views

filter search result with custom post type meta key

The default search returns all post types, which is what I want. But for one specific custom post type I want to limit the result to a specific meta key value (a custom date that must be greater than ...
6
votes
3answers
2k views

Modify Taxonomy pages to exclude items in child taxonomies

I found this question: Theres a way to use $query->set('tax_query' in pre_get_posts filter? which seems to indicate that yes, you can alter the taxonomy query on taxonomy archives via ...
4
votes
1answer
506 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 | ...
3
votes
2answers
3k views

Can I exclude a post by meta key using pre_get_posts function?

I see many people prefer to use pre_get_posts hook instead of query_posts. The code below works and shows all posts which have meta key "featured" function featured_posts( $query ) { if ( ...
3
votes
2answers
2k views

Media library to list images only user uploaded

I want to list images for only user uploaded image. Here is the scenario: Using the image uploader on front end using iframe. I have added upload_files cap to subscriber level users and want them ...
2
votes
1answer
1k views

Create multiple Search functions for posts / custom post types and everything

I'm currently working on a search function in a wp site. The plan is to have a search function on the news page which just searches posts. I'm achieving this by adding the below to my functions.php ...
1
vote
1answer
974 views

pre_get_posts Remove posts based on meta value with 'post__not_in'

I am attempting to alter the query of pages where the post meta value of _members_access_role does not meet that of the currently logged-in user. Here is what I have so far: // Get all posts with the ...
0
votes
1answer
1k views

Help With issue on pre_get_posts filter in taxonomy

I create a function to filter the taxonomy query with this code add_action('pre_get_posts', 'custom_taxonomy_query'); $option_taxposts_per_page = get_option('tax_posts_per_page'); function ...
2
votes
1answer
156 views

Custom post type with tags

My understanding is, that custom post types can share tags with the posts also. I have created tags and applied them to a blog post and a custom post type. Yet when I click on a tag it only returns ...
1
vote
2answers
130 views

Why is 'pre_get_posts' having no effect?

I'm trying to use pre_get_posts for the first time without any success. It's a very simple piece of code on the index page of an otherwise empty theme, purely to get the hang of the action hook: ...
0
votes
2answers
889 views

How Do I Use WP_Query to Run This Database Query as Search Result?

I have created a search box with a drop down box of 4 options to customize the search: A general default search, no filter A custom search according to the post title for a custom post type only. A ...
0
votes
3answers
2k views

Query with pre_get_posts to get pagination

After pretty much getting everything working with query_posts, I noticed my pagination wasn't running. I was told pre_get_posts should be the solution. My problems: I don't know how to pass all my ...
0
votes
2answers
2k views

Displaying custom post type on category pages but not on blog listings

I think this is a fairly simple issue but I haven't got my head around it. Question: how do I get my custom posts to show only on the category archives, but not in the blog post listing page? ...
2
votes
1answer
1k views

Search ONLY by meta key / meta values

I'm almost there on this one. On functions.php i have this: function base_home_product_post_type( $query ) { if($query->is_search() && $_POST['box'] == 'sku') { ...
2
votes
1answer
956 views

Custom search filter causes menu and query_posts problems

I'm using a custom search filter (using my custom query var type), like so: function fteh_pre_get_posts( $query ){ if( isset( $query->query_vars['type'] ) ) $types = explode( ',', ...
1
vote
2answers
475 views

Single page theme that uses pages for the content

I'm making a one page site. On the page I want to run WP_Query three or four times to pull in those 3-4 pages. The page looks a bit like this: <div class="row"> <?php $args = array( ...
1
vote
3answers
3k views

$query->query_var['post_type'] not set for pages

Bulding off of this response from @kaiser about being able to filter by post types on a search page, I wanted to be able to automatically add-in all of the public post types available. So, after some ...
1
vote
1answer
467 views

WP_Post_List_Table::get_views - Have post counts account for filters?

Toward the top of the edit.php screens there is a list that displays post statuses along with the post count. I believe this is generated by WP_Post_List_Table::get_views. For example All (7) | ...
0
votes
1answer
113 views

Run search query again without pagination gives no results?

Update: This question isn't really relevant anymore. I switched some code, so now the filtering works. Looks like the query wasn't working, because I triggered the indexTaxonomyCounts function from ...
0
votes
2answers
121 views

How to second orderby in “pre_get_posts” by meta value or combine single date and time to timestamp

This is somewhat of a continued problem from another question that I had asked. Here is the original post. pre_get_post filter returns results when there should not be Since then I added an orderby ...
0
votes
2answers
141 views

pre_get_post filter returns results when there should not be

I am trying to filter a CPT by an array of post ids using pre_get_post filter within my plugin. Everything works fine when there are post that match the ids, but when there are no post that match the ...
0
votes
1answer
237 views

Can't sort order of wp_query with 2 meta keys

I'm trying to display custom post types by excluding the sold ones but also by displaying them in alphabetical order from the county meta_key. I'm trying to use pre_get_posts but I'm not sure that ...
0
votes
1answer
197 views

Replace query_posts with pre_get_posts

I have a static front page that checks if the user is logged in, and if he/she is a number of x posts from a specific category is displayed. For that I was using this code: ...
0
votes
1answer
226 views

Insert/sticky specific post into Loop at specific location

I'm having a hard time determining the first direction to take with this. I'm trying to write a plugin that allows users to pick a location for a post to be inserted into the homepage. For example a ...
-3
votes
1answer
538 views

How to use pre_get_posts?

I am having a problem with the custom category template pagination and i search and read that using pre_get_posts fix the pagination problem. I read the pre_get_posts information in the WordPress ...