自定义搜索查询

时间:2015-09-17 17:18:35

标签: php mysql wordpress

我制作了一个自定义搜索表单,一个使用带有类别的自定义帖子类型的查询,自定义分类和元字段以及用于显示它们但无法显示结果的搜索模板。我正在使用Wordpress。我已经改变了使用pre_get_posts进行查询的方法,但现在却无法显示结果。所有变量都传递给query_vars。

以下是我的代码:

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

    if (is_home() || is_search() || is_archive() || is_page( 'search' ) )
    {
       $query->set('post_type', 'yacht');
       $query->set('posts_per_page', 1000);
       $query->set('s', '' );
       $meta_query = array();


       $meta_query[] = array(
         array(
            'key'   => 'yachts_length',
            'value'   => array($_GET['min_length'], $_GET['max_length']),
            'compare' => 'BETWEEN',
            'type'  => 'NUMERIC',
          ),
         array(
            'key'   => 'yachts_price',
            'value'   => array($_GET['min_price'], $_GET['max_price']),
            'compare' => 'BETWEEN',
            'type'  => 'NUMERIC',
         ),
         array(
            'key'   => 'yachts_year',
            'value'   => array($_GET['min_year'], $_GET['max_year']),
            'compare' => 'BETWEEN',
            'type'  => 'NUMERIC',
         )
        );
       $meta_query['relation'] = 'AND';
      $query->set('meta_query',$meta_query);
    }

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

使用以下形式输出query_vars:

Array
(
    [cat] => 0
    [page] => 0
    [pagename] => search
    [manufacturer] => 0
    [min_length] => 3
    [max_length] => 100
    [min_price] => 500
    [max_price] => 999999
    [min_year] => 1970
    [max_year] => 2015
    [error] => 
    [m] => 
    [p] => 0
    [post_parent] => 
    [subpost] => 
    [subpost_id] => 
    [attachment] => 
    [attachment_id] => 0
    [name] => search
    [static] => 
    [page_id] => 0
    [second] => 
    [minute] => 
    [hour] => 
    [day] => 0
    [monthnum] => 0
    [year] => 0
    [w] => 0
    [category_name] => 
    [tag] => 
    [tag_id] => 
    [author] => 
    [author_name] => 
    [feed] => 
    [tb] => 
    [paged] => 0
    [comments_popup] => 
    [meta_key] => 
    [meta_value] => 
    [preview] => 
    [s] => 
    [sentence] => 
    [fields] => 
    [menu_order] => 
    [category__in] => Array
        (
        )

    [category__not_in] => Array
        (
        )

    [category__and] => Array
        (
        )

    [post__in] => Array
        (
        )

    [post__not_in] => Array
        (
        )

    [tag__in] => Array
        (
        )

    [tag__not_in] => Array
        (
        )

    [tag__and] => Array
        (
        )

    [tag_slug__in] => Array
        (
        )

    [tag_slug__and] => Array
        (
        )

    [post_parent__in] => Array
        (
        )

    [post_parent__not_in] => Array
        (
        )

    [author__in] => Array
        (
        )

    [author__not_in] => Array
        (
        )

    [orderby] => menu_order
    [order] => ASC
    [post_type] => yacht
    [posts_per_page] => 1000
    [meta_query] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [key] => yachts_length
                            [value] => Array
                                (
                                    [0] => 3
                                    [1] => 100
                                )

                            [compare] => BETWEEN
                            [type] => NUMERIC
                        )

                    [1] => Array
                        (
                            [key] => yachts_price
                            [value] => Array
                                (
                                    [0] => 500
                                    [1] => 999999
                                )

                            [compare] => BETWEEN
                            [type] => NUMERIC
                        )

                    [2] => Array
                        (
                            [key] => yachts_year
                            [value] => Array
                                (
                                    [0] => 1970
                                    [1] => 2015
                                )

                            [compare] => BETWEEN
                            [type] => NUMERIC
                        )

                )

            [relation] => AND
        )

    [ignore_sticky_posts] => 
    [suppress_filters] => 
    [cache_results] => 1
    [update_post_term_cache] => 1
    [update_post_meta_cache] => 1
    [nopaging] => 
    [comments_per_page] => 50
    [no_found_rows] => 
)

0 个答案:

没有答案