Support

Account

Home Forums General Issues Search form to query multiple acf fields

Solving

Search form to query multiple acf fields

  • Hi everyone,

    I’ve been googling and searching the forums and docs on this site and still at a loss.
    I’ve used this page http://www.advancedcustomfields.com/resources/query-posts-custom-fields/ but am baffled at knowing where to plug in the code and link in a front end search from it.

    Basically I want people to be able to search for the custom post type as well as price, location, bedrooms. Has anyone found a way to do this?

  • I have used FacetWP with success it is compatible with ACF and many other plugins.

  • 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);
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Search form to query multiple acf fields’ is closed to new replies.