具有多个文本字段的自定义WordPress搜索

时间:2019-07-02 19:41:57

标签: wordpress

我需要创建一个自定义WordPress搜索表单,其中包含多个文本输入字段。每个文本字段都应搜索其相应的元关键字。前端看起来像这样:

enter image description here

搜索表单:

<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>

“名称”字段应仅搜索帖子标题。其余输入字段将搜索特定的自定义元键。使用多个字段时,它将使用“ AND”关系。

这可能吗?这是我尝试过的方法,但是它不会搜索名称(“ s”)是否为空,并且似乎完全不受我在“家乡”或“分支”的自定义字段中输入的内容的影响。

// 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;


}

上面的查询在尝试搜索时如下所示:

site.com/?s=john+doe&branch=army&hometown=new+york&post_type=veterans

1 个答案:

答案 0 :(得分:2)

检查搜索中是否有类似的字词

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

尝试将每种类型的搜索存储在var中,然后使用搜索中的所有项目构建自定义查询。即:

<?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

我尚未对此进行测试,但是它应该可以工作...也许需要一些改进。测试一下,然后回到我身边:)