I've asked a related question previously, but I think I've finally worked out the crux of my issue.

I'm using Advanced Custom Fields and have a number of custom fields attached to my post type. I'm trying to alter the URL to receive posts via their custom field and value.

And I'm getting really close. This is what's currently in my functions.php:

add_action('pre_get_posts', 'my_pre_get_posts');

function my_pre_get_posts( $query ) {
    if( is_admin() ) { return; }

    $meta_query = $query->get('meta_query'); // get original meta query

        if( isset($_GET['type']) ) {                        

        $type = '"' . $_GET['type'] . '"';      

        $meta_query[] = array(
            'key'       => 'type',
            'value'     => $type,
            'compare'   => 'LIKE',
    $query->set('meta_query', $meta_query); // update the meta query args
    return; // always return

Now because ACF uses serialized arrays when dealing with multiple values in custom fields, I'm using the LIKE query. The type custom field actually accepts multiple values, and I'm able to display posts by one type (i.e: women) successfully.


But when trying to retrieve multiple types (i.e: women,men,boys), this doesn't work.


Now I realise I'm working with serialized arrays here, and if I wanted it to query multiple type's I'd have to explode that $type variable, but that doesn't seem to be working either.

If anyone can help, I'm desperate for a solution. Thanks!

EDIT | Multiple times for each type:

$meta_query[] = array(
    'relation'  => 'OR',
        'key'       => 'type',
        'value'     => 'men',
        'key'       => 'type',
        'value'     => 'women',
        'key'       => 'type',
        'value'     => 'boys',
  • How had you tried to explode variable exactly? Really querying against serialized data is trouble in general, best avoided.
    – Rarst
    Jan 11, 2014 at 16:40
  • @Rarst I tried to explode it like so: $type = explode(',', '"' . $_GET['type'] . '"');. I realise that it isn't the best solution. I read your response to a similar post here: wordpress.stackexchange.com/questions/25945/…
    – realph
    Jan 11, 2014 at 16:44
  • Meta queries are slow at the best of times. I would strongly advise avoiding testing multiple meta values in the same query. You may find performance starts to tank. Feb 9, 2016 at 15:47

2 Answers 2


I just figured out myself how to do it

Take a look at this example:

$args = array();
$args['relation'] = 'OR';
foreach ( $tag_ids as $t ) {
    $args[] = array(
        'key'     => 'afz_entry_tags',
        'value'   => serialize( strval( $t->id ) ),
        'compare' => 'LIKE'

$query->set( 'meta_query', $args );

I am getting $tag_ids with a normal get_results.

Then i create the first item of the array for meta_query in order to define the OR i need.

Then i iterate through the collection of objects i received from get_results and create as many arrays for the meta_query as i need.

It works like a charm.


LIKE does not take array value. Your options probably are:

  1. Add meta query for each type, might not play well since you are already appending this stuff and would need OR clause.
  2. Use REGEXP compare and glue multiple types into regular expression, might be seriously slow to run.
  • How would I go about adding a meta query for each type? Sorry, I'm so trying to wrap my head around how this works.
    – realph
    Jan 11, 2014 at 16:56
  • @realph $meta_query[] = ... multiple times (for each type) plus $meta_query['relation'] = 'OR'
    – Rarst
    Jan 11, 2014 at 16:58
  • So, like the edit I've made to the original post above??
    – realph
    Jan 11, 2014 at 17:06
  • @realph nested too deep, see docs in Codex.
    – Rarst
    Jan 11, 2014 at 17:09
  • Looking at the Codex but can't work out what I'm doing wrong. I'll keep trucking on with it and try and post back with the correct solution.
    – realph
    Jan 11, 2014 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.