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

2
votes
1answer
168 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
439 views

Ordering by meta_key

I'm trying to order the main query on the 'Jobs' post type archive page. I want to order them by a meta_key so the ones which have a value (i.e. are checked as Featured') are shown first. Here's what ...
1
vote
1answer
193 views

Custom Order in WP Query

I'm trying to implement my own query ordering, here is what I came up with... $query = new WP_Query('post_type=contentboxes&include=' . $contentboxes . '&order=ASC&orderby=include' ); ...
0
votes
2answers
991 views

pre_get_posts and set

I have some functions into my pre_get_posts filter. So, the problem is that when I set a meta_query, all the menus disappear from the page. function propersearchfilter($query) { $taxarray = ...
3
votes
2answers
4k 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 ( ...
0
votes
1answer
501 views

how to restrict posts_request filter to the main query only

I'm trying to run custom sql for search results, and I'm having trouble limiting the filter to the main query without running a redundant query. I'm successfully getting results via the following: ...
1
vote
2answers
543 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( ...
0
votes
2answers
3k 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
1answer
461 views

Order posts by (hierarchical custom) taxonomy terms and term children

The scenario a custom post type wiki a (hierarchical) custom taxonomy topics a page template archive-wiki.php The situation Posts show up and get ordered by post_date (which is the default). The ...
0
votes
1answer
549 views

How to sort custom post type posts in default order by multiple fields?

I'm with a non-profit that paid for development of a custom post type plugin that will help us track endangered plants that we're keeping in cultivation, rescued from development. We're mostly happy ...
0
votes
2answers
507 views

How to apply pre_get_posts to a custom query?

How can I apply pre_get_posts to a custom query? For example, if I have: $custom_query = new WP_Query(...) //code here How can I apply the pre_get_posts for this $custom_query?
2
votes
1answer
1k views

pre_get_posts on a page

Is it possible to edit the main query on a page using the pre_get_posts filter? I tried but it doesn't seem to work. function pre_wp_queries( $query ) { // only trigger on main qyery if ( ...
2
votes
2answers
307 views

Splitting the main query in multiple loops with query_posts and/or pre_get_posts?

I have a custom post type 'events', to which I have associated a custom taxonomy 'calendar'. The calendar has 12 fixed terms, for each month of the year ( 'august', 'october', 'november, etc.). I'm ...
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') { ...
0
votes
2answers
934 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 ...
1
vote
1answer
1k 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 ...
1
vote
2answers
313 views

Distinction on meta value on pre_get_posts

I've got a special "position" meta field for posts which can be between 0 and 37. 0 Is ignored, and everything else can have obviously any number between and including 1 and 37. But this of course ...
2
votes
1answer
258 views

Getting the different post statuses + count like in edit.php, in a custom submenu page

I'm trying to create a submenu page for reordering posts/pages. Right now I'm trying to include the list that display the different post statuses, for example: All (5) | Published (4) | Draft (1) I ...
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
119 views

Showing posts from different categories and from custom post type

On my main page, I want to show posts from 3 categories, and 1 custom post type. Is there a way to get them together in one pre_get_posts function? Or do I need to query separately - once for the ...
3
votes
2answers
188 views

How to target the default Recent Posts and Recent Comments widgets with pre_get_posts?

I added the following to my functions.php: add_action('pre_get_posts', 'keyl_get_emp_posts'); function keyl_get_emp_posts($query) { if ($query->is_main_query()) ...
0
votes
3answers
3k 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
575 views

Check post_date in pre_get_posts

I want to change the order of a feed using pre_get_posts filter. But I want to exclude dates older than today. I see how one could do that if the date were in a meta key, but the date is in ...
2
votes
1answer
986 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
1answer
517 views

Custom post type search using $_SESSION and pre_get_posts

I've been hacking all day at a custom post type search/filter system. So far I have this: function kdev_property_query($query) { if(isset($_POST['rooms_n'])) $_SESSION['rooms_n'] = ...
2
votes
1answer
1k views

Adding meta_key via pre_get_posts causes navigation to disappear

I'm writing a custom function that allows the posts to be sorted based on a custom field. When the user submits the "sort by" form, the meta_key (as well as orderby and order) parameters by which the ...
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 ...
0
votes
2answers
790 views

using pre_get_posts for search results not found

function hotlinkers_wp_query($query) { if ( !$query->is_admin && $query->is_search) { $search_query = str_replace('-',' ', $query->query_vars['s']); ...
3
votes
2answers
3k 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 ...
73
votes
3answers
47k 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 ...
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" ...
1
vote
1answer
3k views

Using pre_get_posts to set posts per page, how do I?

I am trying to use pre_get_posts to set posts per page for a single term within a taxonomy. One thing that is throwing me off is setting the term to apply the pre_get_posts to. Here's my code: ...
8
votes
3answers
3k 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
534 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 | ...
0
votes
1answer
3k views

Can I use $query->set() (in a pre_get_posts() hook) with a custom taxonomy in WP 3?

I can do something like the following with a standard category: $query->set('category__not_in', $term_id); But how do I do the same with a custom taxonomy term? I'm using Wordpress 3.3.1 if it ...
0
votes
1answer
207 views

Setting post_id for single.php based on URL without a redirect

I'm migrating a Joomla! database to Wordpress and the website has to remain exact regarding URLs. The Joomla site has its posts with .html extensions, and the importer I created maps the Joomla file ...
1
vote
1answer
504 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
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 ...
0
votes
1answer
809 views

Using a pre_get_posts filter to search for multiple strings on all meta values

Using a pre_get_posts filter to search a given string on all meta_values: $query->set('meta_value', 'first string'); debug: AND ( (CAST(wp_postmeta.meta_value AS CHAR) = 'first string') ) ...
1
vote
1answer
2k views

How to alter query order direction using $query->set('order', 'ASC'); inside a pre_get_posts filter?

According to: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters Default order is DESC and default orderby is date. In a custom plugin, plugin is applying the following ...
6
votes
2answers
7k 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 ...
3
votes
2answers
1k views

pre_get_posts OR relation between taxonomy and author

I have a custom taxonomy called coauthor. On the edit.php screen, I am trying filter and only show posts authored by the current user OR posts coauthored by the current user. I can get the posts ...
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){ ...
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
3k views

pre get posts changing the query

i need to change the posts in taxonomy.php page. I have a meta_key which i want to compare to meta value. But currently my code is not returning anything. I am sure i am missing something. Please let ...
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 ...
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 ...