使用自定义元字段搜索表单

时间:2015-08-31 12:38:22

标签: php wordpress forms

我正在尝试获取位于Wordpress首页的自定义表单,其中包含自定义帖子类型,类别,自定义分类和自定义元字段的字段。我想要实现的两件事:

1st:根据搜索,将从选择字段中选择的值与来自特定元字段或/和来自/与分类/类别的值进行比较。

第二:提交表单时,未选择的值将不会包含在搜索中。

我在构建查询时遇到问题,如果我必须制作不同的搜索页面模板或者我可以使用默认搜索模板,我也很困惑。

以下是我目前使用的所有内容:

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

  if ( $query->is_search ) {
    $query->set( 'post_type', array('post', 'yacht') );
    }

  return $query;
};
add_action('pre_get_posts', 'marinedeals_filter_search');

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

  if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    // Get all your post types
    $post_types = array( 'post', 'yacht' );

    $query->set( 'post_type', $post_types );
  }

  return $query;
}
add_action( 'pre_get_posts', 'marinedeals_add_custom_types_to_tax' );

function marinedeals_custom_search_query( $query ) {
  if ( !is_admin() && $query->is_search ) {
    $query->set('meta_query', array(
      array(
        'key' => '__meta_key__',
        'value' => $query->query_vars['s'],
        'compare' => 'LIKE'
      )
    ));
  };
}
add_filter( 'pre_get_posts', 'marinedeals_custom_search_query');

function no_content_while_no_excerpt()
{
  remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
}
add_action( 'init', 'no_content_while_no_excerpt' );

function getRewriteRules() {
    global $wp_rewrite; // Global WP_Rewrite class object
    return $wp_rewrite->rewrite_rules();
}

function add_query_vars($aVars) {
    $aVars[] = "manufacturers";    // represents the name of the product category as shown in the URL
    $aVars[] = 'length';
    $aVars[] = 'price';
    $aVars[] = 'year';
    $aVars[] = 'yacht';
    return $aVars;
}

add_filter( 'query_vars', 'add_query_vars' );

function marindeals_header_search_form()
{
?>
  <div id="header-form">
    <form id="yachtsearchform" action="<?php echo esc_url(home_url( '/search' )); ?>/" method="get" >
      <div class="form-container">
        <h4 class="yacht-titles">Κατηγορία</h4>

        <?php
        $args = array(

          'orderby'            => 'menu_order',
          'show_option_all'    => 'ΟΛΕΣ',
          'order'              => 'ASC',
          'hide_empty'         => 0,
          'exclude'            => -1,
          'selected'           => -1,
          'name'               => 'cat',
          'class'              => 'postform',
          'taxonomy'           => 'category',
        );

        wp_dropdown_categories( $args ); ?>

        <h4 class="yacht-titles">Κατασκευαστής</h4>

        <?php

        $args = array(

          'orderby'            => 'menu_order',
          'show_option_all'    => 'ΟΛΟΙ',
          'order'              => 'ASC',
          'hide_empty'         => 0,
          'exclude'            => -1,
          'selected'           => -1,
          'name'               => 'manufacturer',
          'class'              => 'postform',
          'taxonomy'           => 'manufacturers',
        );

        wp_dropdown_categories( $args );

        ?>

        <h4 id="yacht-length-title" class="yacht-titles">Μήκος</h4>
        <label>Από:
          <select name="length" class="yacht-length-select">
            <option value="3">3</option>
            <option value="5">5</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="12">12</option>
            <option value="14">14</option>
            <option value="17">17</option>
            <option value="21">21</option>
          </select>
        </label>
        <label>Έως:
          <select name="length" class="yacht-length-select">
            <option value="21">21</option>
            <option value="17">17</option>
            <option value="14">14</option>
            <option value="12">12</option>
            <option value="10">10</option>
            <option value="9">9</option>
            <option value="8">8</option>
            <option value="7">7</option>
            <option value="5">5</option>
            <option value="3">3</option>
          </select>
        </label>
        <input type="radio" name="unit" value="meters" id="unit-meters" checked="checked">(m)
        <input type="radio" name="unit" value="feet" id="unit-feet">(ft)
        <h4 id="yacht-price-title" class="yacht-titles">Τιμή (€)</h4>
        <label>Από:
          <select class="yacht-price-select" name="price">
            <option value="500">500</option>
            <option value="1000">1000</option>
            <option value="5000">5000</option>
            <option value="10000">10000</option>
            <option value="15000">15000</option>
            <option value="20000">20000</option>
            <option value="25000">25000</option>
            <option value="30000">30000</option>
            <option value="40000">40000</option>
            <option value="50000">50000</option>
            <option value="80000">80000</option>
            <option value="110000">110000</option>
            <option value="140000">140000</option>
          </select>
        </label>
        <label>Έως:
          <select class="yacht-price-select" name="price">
            <option value="1000">1000</option>
            <option value="5000">5000</option>
            <option value="10000">10000</option>
            <option value="15000">15000</option>
            <option value="20000">20000</option>
            <option value="25000">25000</option>
            <option value="30000">30000</option>
            <option value="40000">40000</option>
            <option value="50000">50000</option>
            <option value="80000">80000</option>
            <option value="110000">110000</option>
            <option value="140000">140000</option>
          </select>
        </label>
        <h4 id="yacht-year-title" class="yacht-titles">Έτος</h4>
        <label>Από:
          <select class="yacht-year-select" name="year">
            <option value="1970">1970</option>
            <option value="1971">1971</option>
            <option value="1972">1972</option>
            <option value="1973">1973</option>
            <option value="1974">1974</option>
            <option value="1975">1975</option>
            <option value="1976">1976</option>
            <option value="1977">1977</option>
            <option value="1978">1978</option>
            <option value="1979">1979</option>
            <option value="1980">1980</option>
            <option value="1981">1981</option>
            <option value="1982">1982</option>
            <option value="1983">1983</option>
            <option value="1984">1984</option>
            <option value="1985">1985</option>
            <option value="1986">1986</option>
            <option value="1987">1987</option>
            <option value="1988">1988</option>
            <option value="1999">1999</option>
            <option value="2000">2000</option>
            <option value="2001">2001</option>
            <option value="2002">2002</option>
            <option value="2003">2003</option>
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2009</option>
            <option value="2010">2010</option>
            <option value="2011">2011</option>
            <option value="2012">2012</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015">2015</option>
          </select>
        </label>
        <label>Έως:
          <select class="yacht-year-select" name="year">
            <option value="1970">1970</option>
            <option value="1971">1971</option>
            <option value="1972">1972</option>
            <option value="1973">1973</option>
            <option value="1974">1974</option>
            <option value="1975">1975</option>
            <option value="1976">1976</option>
            <option value="1977">1977</option>
            <option value="1978">1978</option>
            <option value="1979">1979</option>
            <option value="1980">1980</option>
            <option value="1981">1981</option>
            <option value="1982">1982</option>
            <option value="1983">1983</option>
            <option value="1984">1984</option>
            <option value="1985">1985</option>
            <option value="1986">1986</option>
            <option value="1987">1987</option>
            <option value="1988">1988</option>
            <option value="1999">1999</option>
            <option value="2000">2000</option>
            <option value="2001">2001</option>
            <option value="2002">2002</option>
            <option value="2003">2003</option>
            <option value="2004">2004</option>
            <option value="2005">2005</option>
            <option value="2006">2006</option>
            <option value="2007">2007</option>
            <option value="2008">2008</option>
            <option value="2009">2009</option>
            <option value="2010">2010</option>
            <option value="2011">2011</option>
            <option value="2012">2012</option>
            <option value="2013">2013</option>
            <option value="2014">2014</option>
            <option value="2015" selected>2015</option>
          </select>
        </label>
        <button type="submit" class="button" id="search-form-button">Αναζήτηση</button>
      </div>
    </form>
  </div>
<?php
}

0 个答案:

没有答案