Support

Account

Home Forums General Issues Create Search on Checkbox options

Helping

Create Search on Checkbox options

  • I’m trying to create a search feature that will post the checkbox options on a page that will be selected by the user which will then be used to search our database.

    The custom fields are built. Currently I am unable to get the fields to display. I’ve done a simple vardump following the instructions here on AFC resource – but obviously, I’m missing something. I need to be able to assign checkboxes to each item – then search on the checked ones. Here is what I have:

    echo ‘<h5>Garage Type</h5>’;
    $garage_type = ”;
    $garage_type = get_post_meta(get_the_ID(),’garage_type’,true);
    $garage_type = get_field(‘garage_type’);
    if($garage_type)
    {
    echo ‘

      ‘;
      foreach($garage_type as $garage_types)
      {
      echo ‘

    • ‘ . $garage_types . ‘
    • ‘;
      }
      echo ‘

    ‘;
    }
    var_dump($garage_type);

    And here is What I get:

    Garage Type

    Courtyard Entry

    array(1) { [0]=> string(15) “Courtyard Entry” }

    Any ideas? Thanks

  • Hi @debschmidt,

    Thanks for the post.

    To create a post search filtered by custom field values, you will need to create a custom query using the WP_Query class and then include a meta_query with custom field meta like so:

    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'event',
    	'meta_query'	=> array(
    		'relation'		=> 'OR',
    		array(
    			'key'		=> 'checkbox_name',
    			'value'		=> 'value_1',
    			'compare'	=> 'LIKE'
    		),
    		array(
    			'key'		=> 'checkbox_name',
    			'value'		=> 'value_2',
    			'compare'	=> 'LIKE'
    		)
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Create Search on Checkbox options’ is closed to new replies.