Home Forums ACF PRO Filtering posts with multiple select


Filtering posts with multiple select

  • Hello and thank you for this wonderful plugin!

    I use a custom post type and I need to filter them by type, the type being a “Select” field called “tipo_lemma”. I have selected the option “Select multiple” values” because some posts have more than one type.

    Now, I need to show all the posts that share a certain type, both those which have a single type and those which have multiple value.
    For example, if I have:
    post1 = typeA
    post2 = typeA, typeB
    If I filter by “typeA” I need to see both of them. If I filter by “typeB” just the second one.

    If I use this code, it shows only those with a single type, while those with more than one type don’t appear at all:

    if (isset($_GET["type"]) && $_GET["type"]!='') {
     	$add_args[meta_key] = 'tipo_lemma';
     	$add_args[meta_value] = $_GET["type"];

    If I use this code, it’s the opposite: the page displays only the posts with more than one type, while those with only one type don’t appear (with some exceptions: strangely it shows also some posts with only one type when there is a link in another wysiwyg field – perhaps because I use a str_replace script, or because the full query is kind of complex):

    if (isset($_GET["type"]) && $_GET["type"]!='') {
        $add_args[meta_query] = array(
                'key' => 'tipo_lemma',
                'value' => serialize(strval($_GET["type"])), 
                /*It's the same with: 'value' => '"'.$_GET["type"].'"',*/
                'compare' => 'LIKE'

    Any advice and suggestions will be greatly appreciated!

  • Hi @galadh

    If the query is like this: &type=typeA,typeB, then you need to add a query for each type listed. It should be something like this:

    if (isset($_GET["type"]) && $_GET["type"]!='') {
        // get the listed types in an array
        $types = explode(',', $_GET[ "type" ]);
        // initialize the meta query
        $add_args['meta_query']['relation'] = 'OR';
        // loop through the types and add it to the query
        foreach( $types as $type ){
            $add_args['meta_query'] []= array(
                'key' => 'tipo_lemma',
                'value' => '"'. $type .'"',
                'compare' => 'LIKE'

    I hope this helps 🙂

  • It came out there was another problem (my fault!): there were almost 300 posts when I switched on the option “Select multiple values”, so I guess all the old posts still have a string in the field “tipo_lemma”, while the new ones have an array whether they have one or more type selected.

    In fact, if I change the option “Return value” from “Label” to “Array” nothing happens, but when I open and save again the old posts, then it works. I just need to re-save all the posts then.

    Thank you for the support! Your code works perfectly, so I’ll mark the question as solved 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Filtering posts with multiple select’ is closed to new replies.