Support

Account

Home Forums Front-end Issues Better query for ACF checkbox values?

Solving

Better query for ACF checkbox values?

  • I want to make a query based on certain checkbox values (7a,7b,8a,8b) in a group (groep).
    I noticed however that this particular query is very slow or sometimes not working when I add more fields.

    Since team members can also appear in different groups (3a, 3c) I cannot merge all the groups together but I have a selection 7a,7b,8a and so on.

    Does anyone have a more efficient query?

    $posts = get_posts(array(
    	'post_type'			=> 'team_member',
    	'posts_per_page'	=> -1,
    	  'meta_query' => array(
    	  'relation' => 'OR',
            			array(
                'key' => 'groep', 
    			'value' => '7a',
                'compare' => 'LIKE'
            ),
    		    	  array(
                'key' => 'groep', 
    			'value' => '7b',
                'compare' => 'LIKE'
            ),
    
    			  	  array(
                'key' => 'groep', 
    			'value' => '8a',
                'compare' => 'LIKE'
            ),
    				  array(
                'key' => 'groep', 
    			'value' => '8b',
                'compare' => 'LIKE'
            )
    	
    	
    		
    		)
    
    ));
  • Hi @avwijk

    I believe you can use the IN compare instead. You should also always try to use the WP_Query class instead of get_posts(). That’s just a wrapper which also bypasses several filters.

    There’s also a few performance tricks one can do to improve performance on the WP_Query class. a single WP_Query actually runs 4 queries per default. We can disable some of these. Below is your query with two of the four queries removed.

    
    $posts = new WP_Query(array(
    	'post_type'			=> 'team_member',
    	'posts_per_page'	=> 500, //never set to -1 as this is an hazard. what if we at some point has 10 000 posts? 
    	'no_found_rows' => true, //since we dont use pagination we can use this
    	'update_post_term_cache' => false, //Since we dont use taxonomies we can use this. If you are going to output taxonomies based on this query remove this line.
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key' => 'groep', 
    			'value' => array('7a', '7b', '8a', '8b'),
    			'compare' => 'IN'
    		)
    	)
    ));
    
  • Thanks, but this didn’t work for me.. it just returns empty rows without any content – in fact, according to the number of the rows all the post in ‘team_member’.

    When I still use $posts = get_posts it breaks the page..

  • Try removing no_found_rows and update_post_term_cache. I guarantee you that get_posts is not the solution in any case 🙂

    Also, remove ‘relation’ => ‘OR’

  • Same result.. 🙁

    in ACF, ‘groep’ is a selector field with values from 1a to 8c. Don’t know if that makes any difference.

  • Ah I see..
    Well if you check the value in the database I think it will be a serialized array?
    If so then IN might not work and you would have to use LIKE.

    I’m not an SQL expert but my guess is that IN would look for these specific values exactly (where as LIKE will just look for the value in the whole db input).

    Another option might be to change your select values to just numbers from 0 and up?

    That way you could easily look for any post with values from 7 – 12 (for example) with the BETWEEN.

    
    $posts = new WP_Query(array(
    	'post_type'			=> 'team_member',
    	'posts_per_page'	=> 500, //never set to -1 as this is an hazard. what if we at some point has 10 000 posts? 
    	'no_found_rows' => true, //since we dont use pagination we can use this
    	'update_post_term_cache' => false, //Since we dont use taxonomies we can use this. If you are going to output taxonomies based on this query remove this line.
    	'meta_query' => array(
    		array(
    			'key' => 'groep', 
    			'value' => array(7, 12),
    			'compare' => 'BETWEEN'
    		)
    	)
    ));
    
Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Better query for ACF checkbox values?’ is closed to new replies.