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

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

最佳答案

检查您的搜索中是否有类似的术语后

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

我还没有测试过这个,但它应该可以工作......也许需要一些改进。测试一下然后回来找我:)

关于wordpress - 具有多个文本字段的自定义 WordPress 搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56859066/

相关文章:

php - 开发插件时 Wordpress 错误 -"You do not have sufficient permissions to access this page."

PHP/MySQL 如何通过管理面板确定表中行的优先级

jquery - 未捕获的 TypeError : $(. ..).accordion 不是 wordpress 中的函数

javascript - 单击导航链接后折叠/隐藏移动导航菜单

jquery - 使用jQuery,WordPress oEmbed和Ajax在div中预览视频

php - 在 Woocommerce 管理产品列表中使特色列可排序

html - 如何在联系表 7 中设置接受标签的样式?

wordpress - CSS @page 不适用

php - woocommerce 产品摘录说明(简短)

php - 在 Wordpress 输出中缩小 CSS