如何在 Wordpress 中为自定义字段创建搜索表单? [英] How to create a search form for custom fields in Wordpress?

查看:23
本文介绍了如何在 Wordpress 中为自定义字段创建搜索表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的搜索表单的代码:

<div class="form-group"><select class="form-control custform"><option>Make (Any)</option><option>2</option><选项>3</选项><选项>4</选项><option>5</option></选择>

<div class="form-group"><select class="form-control custform"><option>模型(任意)</option><option>2</option><选项>3</选项><选项>4</选项><option>5</option></选择>

<div class="form-group"><select class="form-control custform"><option>最低价格</option><option>2</option><选项>3</选项><选项>4</选项><option>5</option></选择>

<div class="form-group"><select class="form-control custform"><option>最高价格</option><option>2</option><选项>3</选项><选项>4</选项><option>5</option></选择>

<div class="form-group"><div class="checkbox"><标签><input id="cars" type="checkbox">汽车

<button type="submit" class="btn btn-primary btn-block btnsearch">查找车辆 </span></表单>

这是它的外观:

我添加了一些不在代码中的类别来展示它应该如何结束.

基本上我对如何在我从头开始编写的 Wordpress 主题中实现这一点一无所知.Make 和 Model 都是自定义字段,Min Price 和 Max Price 显然需要使用某种范围函数来返回价格范围内的结果.

品牌和型号下拉菜单需要仅显示已在自定义字段中输入的可用选项 - 如果有意义的话.

非常感谢任何帮助.

解决方案

您创建自定义搜索表单.表单操作必须是您的特定搜索页面(新模板).

在您的新模板中,您可以使用以下查询:

$meta_query_args = array('关系' =>'AND',//"或"大批('键' =>'_my_custom_key','价值' =>'我正在寻找的价值','比较' =>'='),大批('键' =>'_your_min_model_key','价值' =>1453,'比较' =>'>'),大批('键' =>'_your_max_model_key','价值' =>1923年,'比较' =>'<'));$meta_query = new WP_Meta_Query( $meta_query_args );

比较参数详情:

<块引用>

compare (string) - 要测试的运算符.可能的值是 '=', '!=','>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN',NOT BETWEEN"、EXISTS"(仅在 WP >= 3.5 中)和NOT EXISTS"(也仅在 WP >= 3.5).值REGEXP"、NOT REGEXP"和RLIKE"是在 WordPress 3.7 中添加.默认值为=".

//好的编码

Here's the code for my search form:

<form role="form">
             <div class="form-group">
               <select class="form-control custform">
                                              <option>Make (Any)</option>
                              <option>2</option>
                              <option>3</option>
                              <option>4</option>
                              <option>5</option>
                            </select>
              </div>
              <div class="form-group">
                       <select class="form-control custform">
                                              <option>Model (Any)</option>
                              <option>2</option>
                              <option>3</option>
                              <option>4</option>
                              <option>5</option>
                            </select>
              </div>


                    <div class="form-group">
                         <select class="form-control custform">
                             <option>Min Price</option>
                              <option>2</option>
                              <option>3</option>
                              <option>4</option>
                              <option>5</option>
                            </select>
                     </div>
                        <div class="form-group">
                           <select class="form-control custform">
                                         <option>Max Price</option>
                                          <option>2</option>
                                          <option>3</option>
                                          <option>4</option>
                                          <option>5</option>
                                        </select>

                          </div>

              <div class="form-group">
              <div class="checkbox">
                <label>
                  <input id="cars" type="checkbox"> Cars
                </label>
              </div>
              </div>
              <button type="submit" class="btn btn-primary btn-block btnsearch">
              Find Vehicles   <span class="glyphicon glyphicon-search">  </span>    </button>
</form>

And here's how it should look:

I added a few categories that aren't in the code to show how it should end up.

Basically I'm clueless as to how to implement this in my Wordpress theme that I've coded from scratch. Make and Model are both custom fields and Min Price and Max Price obviously need to use some sort of range function to return results within the price range.

The make and model dropdown menu's need to show only available options that have been entered in the custom field - if that makes sense.

Any help is massively appreciated.

解决方案

You create your custom search form. Form action must be your specific search page(new template).

In your new template, you can use this bellow query:

$meta_query_args = array(
    'relation' => 'AND', // "OR"
    array(
        'key'     => '_my_custom_key',
        'value'   => 'Value I am looking for',
        'compare' => '='
    ),
    array(
        'key'     => '_your_min_model_key',
        'value'   => 1453,
        'compare' => '>'
    ),
    array(
        'key'     => '_your_max_model_key',
        'value'   => 1923,
        'compare' => '<'
    )
);
$meta_query = new WP_Meta_Query( $meta_query_args );

And compare param details:

compare (string) - Operator to test. Possible values are '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'EXISTS' (only in WP >= 3.5), and 'NOT EXISTS' (also only in WP >= 3.5). Values 'REGEXP', 'NOT REGEXP' and 'RLIKE' were added in WordPress 3.7. Default value is '='.

// good coding

这篇关于如何在 Wordpress 中为自定义字段创建搜索表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆