How to create a search form for custom fields in W

2019-02-28 07:07发布

问题:

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.

回答1:

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


回答2:

You want use metabox for posttype like car,ven and 4x4's and price this four metabox add in your posttype so ,easy search post

go to this link to know how to create metabox

http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336

and then you search by meta name like car,ven etc code is here

<ul>
<?php
query_posts('meta_key=your_like_ven key&meta_value=your_value');
?>
<?php if ( have_posts() ) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title();?>
    <?php endwhile; ?>
     <?php endif; ?>
    <?php wp_reset_query();?>
 </ul>