在2个下拉列表中搜索自定义帖子类型页面和自定义字段?

时间:2011-12-05 作者:nathanp

在我的自定义帖子类型页面上,我有用户可以编辑的自定义字段
我想要的是一个具有2个选择下拉列表的搜索表单:一个下拉列表填充所有顶级自定义帖子类型页面,另一个下拉列表缩小结果范围,以显示具有特定自定义字段值的页面。

到目前为止,我还无法通过自定义字段获得要过滤的结果。这是我目前的表单代码:

<form method="get" action="<?php echo get_permalink($properties_search_id); ?>">
            <input type="hidden" name="post_type" value="floor_plan" />
            <ul class="wpp_search_elements">
                <li class="wpp_search_group wpp_group_not_a_group">
                    <ul class="wpp_search_group wpp_group_not_a_group">
                        <li>
                            <label class="wpp_search_label wpp_search_label_bedrooms" for="wpp_search_element_7165">Bedrooms<span class="wpp_search_post_label_colon">:</span></label>
                            <div class="wpp_search_attribute_wrap">
                                <select name="bedrooms" class="bedrooms">
                                    <?php
                                        $metakey = \'number_of_bedrooms\';
                                        $bedrooms = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = %s ORDER BY meta_value ASC", $metakey) );
                                        if ($bedrooms) {
                                        foreach ($bedrooms as $bedroom) {
                                          echo "<option value=\\"" . $bedroom . "\\">" . $bedroom . "</option>";
                                        }
                                        }
                                    ?>
                                </select>
                            </div>
                            <div class="clear"></div>
                        </li>
                        <li>
                            <label class="wpp_search_label wpp_search_label_property_type" for="wpp_search_element_7437">Property Type<span class="wpp_search_post_label_colon">:</span></label>
                            <div class="wpp_search_attribute_wrap">

                                 <?php
                                     $args = array(
                                        \'child_of\'     => 0,
                                        \'sort_order\'   => \'ASC\',
                                        \'sort_column\'  => \'post_title\',
                                        \'hierarchical\' => 2,
                                        \'depth\'         => 1,
                                        \'post_type\' => \'floor_plan\'
                                        );
                                    wp_dropdown_pages( $args );
                                ?>

                            </div>
                            <div class="clear"></div>
                        </li>
                    </ul>
                <div class="clear"></div>
                </li>
                <li class="wpp_search_form_element submit">
                    <input type="submit" value="Search" class="wpp_search_button submit">
                </li>
            </ul>
        </form>
我真的很难弄明白这一点,如果有人能帮忙,我将不胜感激。

1 个回复
SO网友:Matthew Boynes

看起来这个问题已经问了几个月了,但这个问题很好,所以我要把它从坟墓里挖出来。

我解决这个问题的方法是用pre_get_posts 根据提供的信息过滤并添加元查询。这里是解决方案的基本快照,它可以成为插件,也可以加入主题的功能。php文件:

<?php
/**
 * Add a parameter for a custom field search
 */
add_filter(\'query_vars\', \'wpse_35639_search_queryvars\' );
function wpse_35639_search_queryvars( $qvars ) {
    $qvars[] = \'bedrooms\';
    return $qvars;
}


/**
 * Intercept the posts query to add in our meta query if necessary
 */
add_action(\'pre_get_posts\',\'wpse_35639_intercept_search\');
function wpse_35639_intercept_search() {
    global $wp_query;

    if ( ! is_admin() && isset($wp_query->query_vars[\'bedrooms\']) && is_numeric($wp_query->query_vars[\'bedrooms\']) ) {
        # Limit the search to the floor_plan custom post type
        $wp_query->set(\'post_type\', \'floor_plan\');

        # This may seem unconventional, but we\'re setting that this is a search
        # even though WP doesn\'t recognize it as one. This is to leverage the search template
        $wp_query->is_search = true;

        # Set the meta query comparison
        $wp_query->set(\'meta_query\', array(
            array(
                \'key\' => \'number_of_bedrooms\',
                \'value\' => $wp_query->query_vars[\'bedrooms\'],
                \'compare\' => \'=\',
                \'type\' => \'NUMERIC\'
            )
        );
    }
}
?>
注意,这只解决了搜索的自定义字段部分。按页面搜索要容易得多(我想你已经解决了这个问题)。

结束

相关推荐

从_POSTS筛选器添加时,匿名函数在wp_head中执行两次?

我知道这听起来很复杂,所以请耐心听我说。我正在尝试根据当前帖子/页面中的短代码(如果有)添加某些自定义css样式。function load_shortcode_styles($posts){ //function to check short codes here //regex to get id from shortcode(e.g [item id=\"\"] ) //get custom post meta