Support

Account

Home Forums General Issues Search form to query multiple acf fields Reply To: Search form to query multiple acf fields

  • Hi @skellytig

    You can use a plugin like the one @kolhoffmmm suggested. If you want to do it yourself the best way to do it would be to create your own form which would send to the same page (archive) using POST.

    Then you hook into the pre_get_posts filter: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    There you look for the values of your filter and modify the query accordingly.
    Incomplete example:

    
    $meta_query = array();
    
    if( isset($_POST['filtervalue']) && $_POST['filtervalue'] ){
    	
    	$meta_query[] = array(
    		'key' => 'filtervalue',
    		'value' => $_POST['filtervalue'],
    		'compare' => '='
    	);
    	
    }
    
    if( isset($_POST['anotherfiltervalue']) && $_POST['anotherfiltervalue'] ){
    	
    	$meta_query[] = array(
    		'key' => 'anotherfiltervalue',
    		'value' => $_POST['anotherfiltervalue'],
    		'compare' => '='
    	);
    	
    }
    
    $query->set('meta_query', $meta_query);