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

5
votes
6answers
1k views

Sort on meta value but include posts that don't have one

I've been modifying the built in WP search using the pre_get_posts filter, allowing the user to sort the posts (including a bunch of custom post types) by different fields. The problem I'm having ...
0
votes
1answer
933 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 ...
1
vote
1answer
1k views

How to get my Custom Post Type to display in Recent Posts using “pre_get_posts”

I created a Custom Post Type (call it "my-post-type"). I created an archive-my-post-type.php page for displaying all the Custom Post Types. Looking at the "Recent Posts" section of the Sidebar, I ...
0
votes
0answers
98 views

Pre Get Posts fails with more than 1 Post Type

Inside Functions.php function custom_filings_archive( $query ) { if(is_post_type_archive( 'filings' )) $query->set('post_type',array('the-reports','press-release')); return $query; } ...
0
votes
1answer
196 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
195 views

pre_get_posts : 'post__not_in' doesn't work with global variable

I'm trying to set a post__not_in which use a global $popular variable, defined in the index.php. -index.php- $popular[] = 1 //post id=1 $popular[] = 2 //post id=2 $popular[] = 3 //post id=3 ...
0
votes
1answer
221 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 ...
0
votes
1answer
63 views

Exclude page by title for non admins

I have this snippet built from a couple of sources, it works great and if the user is not an admin it hides the page with an id of 243 from being seen in the edit pages section.... add_action( ...
1
vote
2answers
540 views

Force is_search to always TRUE

I'm extending the search functionality by adding custom query vars to the search query object via pre_get_posts so that it'll be able to include tax_query and meta_query as search conditions. ...
4
votes
1answer
1k 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 ...
0
votes
2answers
282 views

pre_get_posts for exclude category

This code works perfectly function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-1' ); } } add_action( ...
0
votes
1answer
47 views

Avoiding page loop

I'm doing a custom page that has multiple wp query calls, the thing is that I don't need the main query that is I don't need anything from the page contents, so in order to save load time I was ...
0
votes
1answer
106 views

Why does this query not SELECT post IDs like a normal query would?

I have a page template, call it page-myblog.php, that I want to display an ordinary loop on. I have created a page 'myblog' in the admin area and it crops up as normal on the front end. I want to ...
2
votes
2answers
1k 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
1answer
306 views

Using database meta_values to calculate new post order using pre_get_posts or a 'request' hook

UPDATE: I have worked out my implementation of the selected answer at the bottom of this post. I'm implementing a sorting algorithm based on Reddit's hotness algorithm, in addition to a time-decay ...
0
votes
1answer
303 views

Custom post types loop on a page template

Hi I'm trying to make a page template that shows a list of a custom post type ordered by a meta key, I easily made it with query_posts but I am trying to do it via pre_get_posts so I made a function ...
0
votes
1answer
117 views

pre_get_posts and BBPress in Swagger Theme

In the theme I'm using there is a custom.php file where the following code issues a pre_get_post function which adds the custom post types to the original query object. I've learned that you cannot ...
0
votes
1answer
296 views

Pre_get_posts Gives 404 on Custom Post Type

I'm trying to filter the query on a sub page called "Ask Question" to list all of the recent questions submitted by users, but I get a 404 when going to the page. Flushing the permalink structure did ...
0
votes
1answer
74 views

Filter multiple different main queries on custom pages

Ok so I have multiple different page templates with a main query that I want to filter the loop for. In my example I try to use a function to filter the main loop of my site and also the main loop for ...
0
votes
3answers
277 views

using pre_get_posts to exclude

I am trying to use pre_get_posts to exclude categories from posting to the homepage. If I do it like so it works just fine $query->set( 'cat', '-13,-7,-19,-12,-24,-21,-14'); that is hard coded ...
0
votes
1answer
156 views

pre_get_posts returning unformatted page

I ran a custom loop the other day to get some twitter information. Since I was running the code in a template and that template was being called from another loop, I was able to get the template data ...
3
votes
1answer
559 views

Using is_main_query to select custom post type on certain page

I've been trying to implement some of the ideas discussed by Andrew Nacin http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/ into my workflow, specifically trying to move away from using ...
1
vote
1answer
336 views

Recommended way to drop a pending query (in pre_get_posts)?

It looks like an e-commerce plugin's core query will be of no use to me in one particular template so I would like to discard it completely in favor of my own WP_Query loops in the template. It's not ...
2
votes
1answer
108 views

When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?

This question is a follow up to a question I recently asked. I've implemented what seemed to be the only solution, and it seems to work (YAY!). I just need to verify that: This is really the only ...
6
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 ...
12
votes
1answer
1k views

Sticky Posts exceed posts per page limit

I'm using pre_get_posts to adjust the number of posts displayed on my homepage. function lifelounge_query_adjust( $query ) { if ( is_home() ) { set_query_var( 'posts_per_page', 12 ); ...
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 ...
2
votes
1answer
153 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
398 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
174 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
943 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
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 ( ...
0
votes
1answer
435 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
458 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
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
1answer
434 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
483 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
496 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
287 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
885 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
946 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
285 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
223 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
114 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
176 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
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
512 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 ...