3
votes

I need to create a custom WordPress search form, with multiple text input fields. Each text field should search its corresponding meta key. Frontend would look like this:

enter image description here

Search form:

<form role="search" action="/" method="get" id="searchform">
   <input type="text" name="s" placeholder="Name"/>
   <input type="text" name="hometown" placeholder="Hometown"/>
   <input type="text" name="branch" placeholder="Branch of Service"/>
   <input type="text" name="birth" placeholder="Birth Year"/>
   <input type="text" name="casualty" placeholder="Casualty Year"/>
   <input type="text" name="location" placeholder="Casualty Location">
   <input type="hidden" name="post_type" value="veterans" />
   <input type="submit" alt="Search" value="Search" />
</form>

The "Name" field should search the post title only. The rest of the input fields would search a specific custom meta key. It would use an "AND" relationship when multiple fields are used.

Is this possible? Here is what I have tried, but it doesn't search if the name ("s") is empty, and it doesn't seem to be affected at all by what I enter into the custom fields for Hometown or Branch.

// register query vars
function sm_register_query_vars( $vars ) {
    $vars[] = 'hometown';
    $vars[] = 'branch';
    return $vars;
} 
add_filter( 'query_vars', 'sm_register_query_vars' );

// pre get posts
 function sm_pre_get_posts( $query ) {

    if ( is_admin() || ! $query->is_main_query() ){
        return;
    }

    if ( !is_post_type_archive( 'veterans' ) ){
        return;
    }

    $meta_query = array();

    // add meta_query elements
    if( !empty( get_query_var( 'hometown' ) ) ){
        $meta_query[] = array( 'key' => 'hometown', 'value' => get_query_var( 'hometown' ), 'compare' => 'LIKE' );
    }

    if( !empty( get_query_var( 'branch' ) ) ){
        $meta_query[] = array( 'key' => 'branch', 'value' => get_query_var( 'branch' ), 'compare' => 'LIKE' );
    }

    if( count( $meta_query ) > 1 ){
        $meta_query['relation'] = 'AND';
    }

    if( count( $meta_query ) > 0 ){
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'sm_pre_get_posts', 1 );


// the search form (display via shortcode)
function sm_search_form( $args ){

$output = '<form id="smform" action="' . esc_url( home_url() ) . '" method="GET" role="search">';

$output .= '<div class="smtextfield"><input type="text" name="s" placeholder="Name" value="' . get_search_query() . '" /></div>';
$output .= '<div class="smtextfield"><input type="text" name="hometown" placeholder="Hometown" value="' . get_search_query() . '" /></div>';
$output .= '<div class="smtextfield"><input type="text" name="branch" placeholder="Branch" value="' . get_search_query() . '" /></div>';

$output .= '<input type="hidden" name="post_type" value="veterans" />';

$output .= '<p><input type="submit" value="Search" class="button" /></p></form>';


return $output;


}

The above query looks like this when attempting to search:

site.com/?s=john+doe&branch=army&hometown=new+york&post_type=veterans
1
Based on the current answer, you should know that the title parameter is for querying posts having exactly the specified title. I.e. Unlike the s parameter which can do a LIKE query.Sally CJ

1 Answers

2
votes

After checking if there are any terms in your search by something like

if($_GET['myfield'])...

try storing each type of your search in a var and then build a custom query with all the items that are in your search. ie :

<?php
$title=$_GET['name'];   // Get the name
$params=[];     // Create an array with all the parameters you've got except the name

function populate_array($term)  // Create a function to populate your array
{
    if ($_GET[$term]) {
        $params[$term] = $_GET[$term];
    }
}
populate_array('hometown');
populate_array('branch');
//(...)


$args=array(    // Initialize your query
        'post_type' =>  'my_post_type',     // Just set your post type if needed
);

if($title){ // If there is a title add it to the query
    $args['title']=$title;
}

if(count($params)>0){. // If there are any params
    $meta=array('relation'=>'AND'); // Because you asked for it
    foreach($params as $param => $value){
        $meta[]=array(      // Adding each meta tou your query
                'key'       =>  $param,  // considering you name your meta as your parameter
                'value'     =>  $value,
                'compare'   =>  '='
        );
    }
    $args['meta_query']=$meta;  // Adding your meta to your main query
}

$query = new WP_Query( $args ); // And now you can request your query with all your parameters

I have not tested this but it should work... Maybe some improvements are needed. Test it and come back to me :)