I am trying to write a WP post query to get all posts which have a certain meta key, but with those which have the key set to a specific value to show first.

The query modifications are in a pre_get_posts function, as I also need to make use of pagination and some taxonomy filters on the page, so multiple loops isn't an option.

Here is the code I have tried to modify the query:

    $query->set('meta_query', array(
        'relation' => 'OR',
        'centre_is' => array(
            'key' => 'community_centre',
            'compare' => 'LIKE',
            'value' => '"'.get_query_var('centre').'"'
        'centre_not' => array(
            'key' => 'community_centre',
            'compare' => 'NOT LIKE',
            'value' => '"'.get_query_var('centre').'"'

    $query->set('orderby', array(
        'centre_is' => 'ASC',
        'centre_not' => 'ASC',
        'title' => 'ASC'

Basically, there are 2 post types, Activity and Community Centre. Activities are associated with a Community Centre, using an ACF Relationship field called community_centre, which stores an array of post IDs of the associated Community Centres (this was originally set up to allow multiple Community Centres per Activity, but this field is now limited to a maximum of 1).

I need to list all Activities (subject to the other taxonomy filters, etc). However, if the query var 'centre' is present (which is a Community Centre post ID), then I need the Activities associated with that Community Centre to appear first in the list. If there are multiple, they should then be ordered by title. The remaining Activities at other Community Centres should be listed after, also ordered by title.

Based on answers I've found on similar questions, I expected my code above would order the posts first based on whether or not the 'community_centre' field contains the 'centre' query var, and then by title.

However, the query is not showing the Activities associated with the given Community Centre first. It appears to group the posts by Community Centre, and within these groups order them by title, but the ordering of the Community Centres is unclear (it is not by title or date).

If I try the query with only the 'centre_is' meta query, or the 'centre_not' meta query, it behaves as expected, returning only the Activities that are/aren't associated with the given Community Centre. The issue seems to be with the ordering logic.

Any help to get this working would be much appreciated.

  • Keep in mind this is a very expensive/slow query, searching and filtering posts via their meta values can put a big strain on the database, and to add insult to injury, the NOT LIKE clause on its own is very expensive/slow. Using a taxonomy named community_centre would be a massive performance and traffic scaling improvement. Also, do you centre_is and centre_not parameters cancel eachother out? – Tom J Nowell Jan 22 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.