[wp-hackers] Re-ordering search results

Andrew Nacin wp at andrewnacin.com
Tue Nov 20 14:39:49 UTC 2012


On Tue, Nov 20, 2012 at 8:44 AM, Tom Barrett <tcbarrett at gmail.com> wrote:

> I have a list of companies on a site, their profiles are stored as a custom
> post type and they have several levels of membership (e.g. free, bronze,
> silver, gold). Their membership is handled with a taxonomy. I want the free
> text search to prioritise gold-package search results over silver (over
> bronze, over free).
>
> Is there a nice hook-able way for me to do this?
>
> Other solutions are to:
>  a) perform multiple searches (ew?)
>  b) loop several times over search result?
>  c) loop once and build my own set of results.
>
> My thinking is that a simpler db query with lots of results would be better
> than 4 more complex queries (but I will need the taxonomy term at some
> point). So I either need to hook into pre_get_posts or loop through
> $wp_query->posts?


You can re-order $wp_query->posts on your own, but that will only be
sufficient
if you aren't using any pagination here. You may also want to look into how
we
may implement smarter ordering in core in the future (possibly 3.6) —
http://core.trac.wordpress.org/ticket/7394. This is possible with the
existing
search and orderby filters.

Nacin


More information about the wp-hackers mailing list