[wp-trac] [WordPress Trac] #30982: Meta query in pre_get_posts

WordPress Trac noreply at wordpress.org
Sat Jan 24 15:17:42 UTC 2015


#30982: Meta query in pre_get_posts
-----------------------------+-----------------------------
 Reporter:  Spidlace         |       Owner:
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Future Release
Component:  Query            |     Version:  4.1
 Severity:  normal           |  Resolution:
 Keywords:                   |     Focuses:
-----------------------------+-----------------------------

Comment (by theMikeD):

 I'm trying to do the same things as OP and have run into the same problem.
 Taking @boonebgorges advice I have a list of post IDs to insert (which I
 get by creating a new WP_Query() and running it). But there is no
 `post__in` in `$query` as used in `pre_get_posts` so I'm not sure where to
 put the IDs.

 `$query->set( 'post__in', $valid_ids );`

 isn't doing it.

 This all seems redundant to me anyway. If I need to run a query to get the
 list of post IDs to use, then I already have the posts I want to use. So
 then I scrapped `pre_get_posts` and tried hooking to `the_posts` which I
 found mentioned in an offhand way after too much googling. `the_posts` is
 called after the query is run.

 So, after checking to see that I'm in the right spot I run a new
 `WP_Query()` to get the results I need. Except that `the_posts` is run
 after `WP_Query->query` so I end up in an endless recursion.

 So then I poked at query.php and discovered that the filter for
 `the_posts` is wrapped in a `suppress_filters` check. So by adding
 `'suppress_filters' => true` to the query being called in the function
 called by the `the_posts` hook, I get exactly what I want.

 I put all this here in case the next guy that comes along find my solution
 useful.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/30982#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list