Support

Account

Home Forums General Issues Filtering by checkboxes – again…

Solved

Filtering by checkboxes – again…

  • Hello there!
    I don’t know how to resolve my issue.

    I want to filter results by checkboxes.

    This is the form:

    <form name="search" action="" method="get">
    <?php
    	$fieldkey = 'field_55d37c9dbdf01';
    	$fields = get_field_object($fieldkey);
    		if ($fields) {
    			foreach ($fields['choices'] as $key => $value) {?>
    				<label><input type="checkbox" name="restaurant_assets[]" value="<?=$key?>" <?php if(in_array($key, $_GET['restaurant_assets'])){  echo "checked='checked'";}?> > <?=$value?></input></label>
    	<?php }
    }?>
       <input type="submit" value="szukaj" />
    </form>

    Form works ok.

    Main problem is with query…

    If I print_r $_GET[‘restaurant_assets’] there is:
    Array ( [0] => breakfast [1] => internet [2] => drink-bar [3] => delivery-takeaways [4] => the-room-mother-and-child )

    This is not working:

    $restaurant_assets = $_GET['restaurant_assets'];
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'	=> 'restaurant',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'restaurant_assets',
    			'value'		=> $restaurant_assets,
    			'compare'	=> 'LIKE'
    		)
    	)
    );
    query_posts($args);
    if (have_posts()) :  while (have_posts()) : the_post();

    Please help.

  • checkbox fields are stored as serialized arrays in the database. Try putting ” around the search value. This is shown, but not very obvious under QUERY POSTS on this page Query http://www.advancedcustomfields.com/resources/checkbox/

    
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'	=> 'restaurant',
    	'meta_query'	=> array(
    		array(
    			'key'		=> 'restaurant_assets',
    			'value'		=> '"'.$restaurant_assets.'"',
    			'compare'	=> 'LIKE'
    		)
    	)
    );
    
  • unfortunately the problem still exists.

    When I put i.e.
    'value' => "air-conditioning",

    everything is ok.

    So I think there is everything about value from GET param.

    This value is also array. Should I explode it earlier?

  • so what your saying is $_GET[‘restaurant_assets’] is an array? If so

    what you need to do is create a nested meta query with a value for each value in $_GET[‘restaurant_assets’]

    
    $meta_query = array('relation' => 'OR');
    foreach ($_GET['restaurant_assets'] as $value) {
      $meta_query[] = array(
      'key' => 'restaurant_assets',
      'value' => '"'.$value.'"',
      'compare' => 'LIKE'
    }
    $args = array(
      'numberposts' => -1,
      'post_type' => 'restaurant',
      'meta_query' => $meta_query
    );
    
  • Yeah!
    Finally it works!

    Thanks for superb help!

    Have a nice day!

  • One more problem… ;/

    I have 16 choices in my custom field called ‘restaurant_assets’.
    If I check more then about 10-11 of theme there are probably too much arrays in meta_query and then an error appears.

    Is there any other solution to filter posts by ACF checkboxes?

    Regards!

  • Yes, that can be a problem. I remember a topic on this forum that used a filter to alter the query where clause to correct this, but I don’t remember what the solution is. I tried doing a search of the forum without much luck.

    There is another solution but not sure you want to try it. I have created a plugin that will convert fields like these, that is arrays stored as serialized arrays, into standard WP meta fields that use multiple database entries. Then instead of searching the ACF field with multiple like meta queries you can search the new field using 'IN' and supply an array. I thinks it’s working, and I’d love for someone to test it out and tell me if there are any bugs. https://github.com/Hube2/custom-field-converter

  • oh great i have solved this also .. john did a mistake in this code . didnt finish array with );

    $meta_query = array(‘relation’ => ‘OR’);
    foreach ($_GET[‘restaurant_assets’] as $value) {
    $meta_query[] = array(
    ‘key’ => ‘restaurant_assets’,
    ‘value’ => ‘”‘.$value.'”‘,
    ‘compare’ => ‘LIKE’
    );
    }

    but thanks john hubner

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

You must be logged in to reply to this topic.