Support

Account

Home Forums General Issues Help with query posts by Meta Key for multiselect

Solved

Help with query posts by Meta Key for multiselect

  • Hey everyone,

    I have a custom query function using ACF meta which looks like this:

        // WP_Query arguments
        $args = array (
            
            'post_type'              => 'staff',
            'post_status'            => 'publish',
            'tax_query' => array(  
                array(  
                    'taxonomy' => 'team',  
                    'field' => 'slug',  
                    'terms' => 'sales-representatives'  
                )  
            ),
    		
            'meta_key'		=> 'state',
            'meta_value'	=> $state_query
            
        );
        

    Where $state_query is an argument for the function, for example “QLD”.

    This works perfect, however some of my posts have multiple values. I took a look in phpmyadmin and this is what they look like:
    a:2:{i:0;s:3:”NSW”;i:1;s:3:”ACT”;}

    So I can see it looks like an array.

    Is there a way to change the query logic so that it uses a LIKE compare?

    I tried adding this:

    		'meta_query' => array(
    			array(
    			'key' => 'state', // name of custom field
    			'value' => $state_query, // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => 'LIKE'
    			)
    		)

    But it didnt work – any ideas?

  • Actually my bad, the part with the like operator fixed it :S

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

The topic ‘Help with query posts by Meta Key for multiselect’ is closed to new replies.