[wp-hackers] What runs first? `pre_get_posts` or `posts_where`?

Ricardo Moraleida moraleida at gmail.com
Fri Aug 31 13:22:16 UTC 2012


Hey,

I'm aware of the differences, but i've just yesterday run into one of those
cases where they need to be combined. I was altering the main search query
to get a meta_value and also filtering a search string to NOT search for
post_title or post_content. This ->
http://wordpress.stackexchange.com/q/63577/7890.

Use case here is this:

I have a multisite installation with 5 sites: 3 of them have the same
structure, parent-theme, post_types, etc, child themes manage css and
access-permissions only. One is for registration/login/redirect only, and
the last one is a kind of aggregator, where we list contents from those 3
in a different manner, intended for admins only.

I need to alter the queries on that last site so that it would combine the
3 other's contents seamlessly, as if they were this site's content, and not
external. I'm reluctant as to make this another child-theme of the master
one that serves the other three because their structure is very different,
but I'm also reluctant on relying on using switch_to_blog() on the
templates to make separate queries and then join them later.

My best shot at this right now is to run a combination of
get_blogs_of_user() and switch_to_blog() inside pre_get_posts and
posts_where, somehow forcing them to get and serve all that content as one
native query-object, against which I'd be able to use paginate_links() and
other functions heavily dependent on the $wp_query global.

Is that any clear? Any thoughts? Am I reinventing the wheel?

Ricardo Moraleida



2012/8/31 Leo germani <leogermani at gmail.com>

> Hi Ricardo,
>
> pre_get_posts runs first. But actually they are very different hooks.
>
> With pre_get_posts you will manipulate the wp_query object, changing its
> properties before the SQL statement is written.
>
> posts_where is to filter the WHERE statement after all the properties of
> the wp_query object were interpreted and the SQL statement is ready to run.
>
> So the first one is easier to use and you have less chances of breaking
> everything. The second one you will manipulate a string, maybe with some
> regexp, its trickier...
>
> I think you should allways try pre_get_posts first and, unless theres no
> way to achieve what you like (rare cases), use posts_where and all the
> other filters (posts_join, etc...)
>
> cheers
>
> Leo,,
>
> On Fri, Aug 31, 2012 at 9:38 AM, Ricardo Moraleida <moraleida at gmail.com
> >wrote:
>
> > Self-explaining title, I think.
> >
> > I'm planning on doing some heavy query-altering as soon as possible on
> page
> > load, so I'm gathering my options, here.
> >
> > Also, are there any other actions like `post_where` to get other parts of
> > the actual query being run against the database?
> >
> > Any heads-up will help.
> >
> > thanks!
> >
> > Ricardo Moraleida
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
> >
>
>
>
> --
> leogermani.com.br
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list