web-dev-qa-db-ja.com

メタデータドロップダウンによるカスタム投稿タイプのフィルタリング

「商談」のカスタム検索ページまたはリストページにドロップダウンフィルタ(使用中の用語が入力されている)を追加しようとしています。現在、私は分類法でこれをしていますが、メタデータ、meta_key、meta_queryを使ってこれを行うのは私にとって新しいことです。

私は周りを検索しましたが、ほとんどの質問は実際にはこのようなあらかじめ決められたフィルタを扱っていません。

私はちょうど http://wordpress.org/extend/plugins/facetioushttp://を見つけましたこの記事からwordpress.org/extend/plugins/advance-wp-query-search-filterhttps://wordpress.stackexchange.com/questions/99119 /ドロップダウンメニュー付きの高度なワードプレス検索 しかし、この作業の他の部分を手に入れたのでプラグインはやり過ぎるかもしれません。

一般的な考え方は、 "op_duration_meta"のすべての値(例: "one-time"、 "short-term"、 "long-term")を取得し、それをドロップダウンにしてリストをソートするか、または他のリストと並べ替えますドロップダウン。

私はURLに何かを手動で追加することに頼らなければならないだろうと思っていました。

事前に助けてくれてありがとう。

<div class="search">

    <form method="get" class="search-form" action="<?php echo home_url(); ?>">
    <div>
        <?php $query_types = get_query_var('post_type'); ?>

        <?php $args15 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'neighborhoods',
            'pad_counts'               => false );?>

        <?php $args16 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'post_tags',
            'pad_counts'               => false );?>


        <?php $args17 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'member_agencies',
            'pad_counts'               => false );?>

        <?php $args18 = array(
            'type'                     => 'opportunities',
            'child_of'                 => 0,
            'parent'                   => '',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 1,
            'hierarchical'             => 1,
            'exclude'                  => '',
            'include'                  => '',
            'number'                   => '',
            'taxonomy'                 => 'category',
            'pad_counts'               => false );?>

        <?php $args19 = array(
            'meta_key'                 =>'op_duration_meta',
            'post_status'              =>'publish',
            'post_type'                =>'opportunities',
            'orderby'                  =>'date',
            'order'                    =>'DESC' );
            ?>

        <div style="min-height:50px;display:block;"><label for="neighborhoods">1. Choose Neighborhood:</label><br/>
            <select name="neighborhoods">
             <option value=""><?php echo esc_attr(__('Select Neighborhood...')); ?></option>

        <?php $taxx1 = get_categories($args15);
        foreach ($taxx1 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>
        <div style="min-height:50px;display:block;">
        <label for="member_agencies">2. OR Choose Member Agency:</label><br/>
        <select name="member_agencies">
         <option value=""><?php echo esc_attr(__('Select Member Agency...')); ?></option>
         <?php $taxx2 = get_categories($args17); foreach ($taxx2 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>
        <div style="min-height:50px;display:block;">
        <label for="category">3. Choose Category:</label><br/>
        <select name="category">
         <option value=""><?php echo esc_attr(__('Select Category...')); ?></option>
         <?php $taxx3 = get_categories($args18); foreach ($taxx3 as $category) {
                $option = '<option value="'.$category->category_nicename.'">';
                $option .= $category->cat_name;
                $option .= '</option>';
                echo $option;
              }
             ?>
        </select>
        </div>

        <div style="min-height:50px;display:block;">
        <label for="duration">3. Choose Duration:</label><br/>
        <select name="duration">
         <option value=""><?php echo esc_attr(__('Select Duration...')); ?></option>
         <?php

              $key_1_value = get_post_meta($args19);

              foreach ($key_1_value as $category) {
                // check if the custom field has a value
                $option = '<option value="Opportunity Duration">';
                $option .= $category;
                $option .= '</option>';
                echo $option;
            }
             ?>
        </select>
        </div>

        <div><input type="hidden" name="post_type" value="opportunities" checked="checked"/>

        <input style="height:24px;display:inline-block" class="search-submit button" name="submit" type="submit" value="<?php esc_attr_e( 'Sort This List', 'prototype' ); ?>" /></div>
    </div>
    </form><!-- .search-form -->

</div><!-- .search -->
1
Chrisdigital

フィルタリング処理の結果をあなたのページのメインオブジェクトにしたいので、pre_get_postsアクションフックを使ってメインクエリを変更する必要があるかもしれません。メインのクエリを実行したままにしてテーマをロードしてからWordPressに言ったほうがよいでしょう。

//Functions for filters
add_action( 'pre_get_posts', 'my_pre_get_post' );
function my_pre_get_post($query){

     //limit to main query, frontend and archive pages. You need another set of checkings, like check if the request is for your custom post type
     if($query->is_main_query() && !is_admin() && $query->is_archive ) {

          $meta_query = array();
          $meta_input = isset($_POST['meta_input']) ? $_POST['meta_input'] : '';

          if(!empty($meta_input)){
              $meta_query[] = array(
                  'key'  => 'your_meta_key',
                  //sanitize $_POST['meta_input'] according with your data type
                  'value'    => $_POST['meta_input'],
              );
          } 
          $query->set('meta_query',$meta_query);
    }

}

meta_query に関する詳細を参照してください。

1
cybmeta