WordPress查询未返回正确的结果

时间:2017-12-29 作者:Mohsin

我正在从查询中获取数据此查询中的数据来自不同的筛选器,每个筛选器都工作正常并获得正确的结果,但当我选择国家/地区时,所有其他筛选器都不会返回任何记录事实上,来自该国/地区的所有记录都会返回,我正在调试此问题大约6个小时,但什么都没有发生任何人请帮助我解决此问题正在执行

我进行查询的函数。

public function pre_get_posts( $q ) {

// check if the user is requesting an admin page 
if ( is_admin() || ! $q->is_main_query() )
    return;

if ( ! is_post_type_archive( \'auto-listing\' ) )
    return;

if ( ! is_search() )
    return;

$meta_query = array();

$year_query[]       = $this->year_query();
$make_query[]       = $this->make_query();
$model_query[]      = $this->model_query();
$condition_query[]  = $this->condition_query();
$odometer_query[]   = $this->odometer_query();
$price_query[]      = $this->price_meta_query();
$body_type_query    = $this->body_type_query();
$country_query          = $this->country_query();
$transmission_query     = $this->transmission_query();
$radius_query[]     = $this->radius_query( $q );

$query_1 = array_merge( $country_query, $year_query, $model_query, 
$condition_query, $price_query, $odometer_query, $transmission_query );
var_dump($query_1);
// if our rasius query fails, fall back to keyword searching
// will fail with no map API key
if( empty( $radius_query[0] ) || ! $radius_query[0] ) {
    $keyword_query[] = $this->keyword_query( $q );
    $query_2 = $keyword_query;
} else {
    $query_2 = $radius_query;
}

// if no keyword
if ( empty( $_GET[\'s\'] ) ) {
            echo \'HERE\';
    $query_1[\'relation\'] = \'AND\';
    $meta_query[] = $query_1;
}

// if keyword
if ( ! empty( $_GET[\'s\'] ) ) {
            echo \'!HERE\';
    $query_2[\'relation\'] = \'OR\';
    $meta_query[] = $query_1;
    $meta_query[] = $query_2;
    $meta_query[\'relation\'] = \'AND\';
}

$q->set( \'meta_query\', $meta_query );

$q->set( \'tax_query\', $body_type_query ); 

$q->set( \'post_type\', \'auto-listing\' ); 

// echo \'<pre>\';print_r($q);


 }
这里是国家查询功能

/**
 * Returns a meta query for filtering by country.
 * @return type
 */
  private function country_query() {
  if ( isset( $_GET[\'country\'] ) && ! empty( $_GET[\'country\'] ) ) {
  $data = array_map( \'sanitize_text_field\', wp_unslash( 
  $_GET[\'country\'] ) );
  return array(
    \'key\'       => \'_al_listing_country\', 
    \'value\'     => $data,
    \'compare\'   => \'IN\'

);
 }
 return array();
}
我不知道我做错了什么,如果有人能跟踪这个问题,那将是一个很大的帮助。

var_export result with country

WP_Query::__set_state( array(
    \'query\' => array(
        \'s\' => \'\',
        \'post_type\' => \'auto-listing\',
        \'model\' => array(
            0 => \'F12 Berlinetta\',
            1 => \'California\',
        ),
        \'condition\' => array(
            0 => \'New\',
            1 => \'Used\',
        ),
        \'min_price\' => \'\',
        \'max_price\' => \'\',
        \'odometer\' => \'\',
        \'within\' => \'\',
        \'country\' => array(
            0 => \'Pakistan\',
            1 => \'United Kingdom\',
        ),
    ),
    \'query_vars\' => array(
        \'s\' => \'\',
        \'post_type\' => \'auto-listing\',
        \'model\' => array(
            0 => \'F12 Berlinetta\',
            1 => \'California\',
        ),
        \'condition\' => array(
            0 => \'New\',
            1 => \'Used\',
        ),
        \'min_price\' => \'\',
        \'max_price\' => \'\',
        \'odometer\' => \'\',
        \'within\' => \'\',
        \'country\' => array(
            0 => \'Pakistan\',
            1 => \'United Kingdom\',
        ),
        \'error\' => \'\',
        \'m\' => \'\',
        \'p\' => 0,
        \'post_parent\' => \'\',
        \'subpost\' => \'\',
        \'subpost_id\' => \'\',
        \'attachment\' => \'\',
        \'attachment_id\' => 0,
        \'name\' => \'\',
        \'static\' => \'\',
        \'pagename\' => \'\',
        \'page_id\' => 0,
        \'second\' => \'\',
        \'minute\' => \'\',
        \'hour\' => \'\',
        \'day\' => 0,
        \'monthnum\' => 0,
        \'year\' => 0,
        \'w\' => 0,
        \'category_name\' => \'\',
        \'tag\' => \'\',
        \'cat\' => \'\',
        \'tag_id\' => \'\',
        \'author\' => \'\',
        \'author_name\' => \'\',
        \'feed\' => \'\',
        \'tb\' => \'\',
        \'paged\' => 0,
        \'meta_key\' => \'\',
        \'meta_value\' => \'\',
        \'preview\' => \'\',
        \'sentence\' => \'\',
        \'title\' => \'\',
        \'fields\' => \'\',
        \'menu_order\' => \'\',
        \'embed\' => \'\',
        \'category__in\' => array(),
        \'category__not_in\' => array(),
        \'category__and\' => array(),
        \'post__in\' => array(),
        \'post__not_in\' => array(),
        \'post_name__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(),
        \'post_status\' => \'publish\',
        \'orderby\' => \'date ID\',
        \'order\' => \'DESC\',
        \'posts_per_page\' => 10,
        \'meta_query\' => array(
            0 => array(
                \'key\' => \'_al_listing_country\',
                \'value\' => array(
                    0 => \'Pakistan\',
                    1 => \'United Kingdom\',
                ),
                \'compare\' => \'IN\',
                0 => array(),
                1 => array(
                    \'key\' => \'_al_listing_model_name\',
                    \'value\' => array(
                        0 => \'F12 Berlinetta\',
                        1 => \'California\',
                    ),
                    \'compare\' => \'IN\',
                ),
                2 => array(
                    \'key\' => \'_al_listing_condition\',
                    \'value\' => array(
                        0 => \'New\',
                        1 => \'Used\',
                    ),
                    \'compare\' => \'IN\',
                ),
                3 => array(),
                4 => array(),
                \'relation\' => \'AND\',
            ),
        ),
        \'tax_query\' => NULL,
    ),
    \'tax_query\' => WP_Tax_Query::__set_state( array(
        \'queries\' => array(),
        \'relation\' => \'AND\',
        \'table_aliases\' => array(),
        \'queried_terms\' => array(),
        \'primary_table\' => NULL,
        \'primary_id_column\' => NULL,
    )),
    \'meta_query\' => false,
    \'date_query\' => false,
    \'post_count\' => 0,
    \'current_post\' => -1,
    \'in_the_loop\' => false,
    \'comment_count\' => 0,
    \'current_comment\' => -1,
    \'found_posts\' => 0,
    \'max_num_pages\' => 0,
    \'max_num_comment_pages\' => 0,
    \'is_single\' => false,
    \'is_preview\' => false,
    \'is_page\' => false,
    \'is_archive\' => true,
    \'is_date\' => false,
    \'is_year\' => false,
    \'is_month\' => false,
    \'is_day\' => false,
    \'is_time\' => false,
    \'is_author\' => false,
    \'is_category\' => false,
    \'is_tag\' => false,
    \'is_tax\' => false,
    \'is_search\' => true,
    \'is_feed\' => false,
    \'is_comment_feed\' => false,
    \'is_trackback\' => false,
    \'is_home\' => false,
    \'is_404\' => false,
    \'is_embed\' => false,
    \'is_paged\' => false,
    \'is_admin\' => false,
    \'is_attachment\' => false,
    \'is_singular\' => false,
    \'is_robots\' => false,
    \'is_posts_page\' => false,
    \'is_post_type_archive\' => true,
    \'query_vars_hash\' => \'75c1fa23a84337dc910c0b799ec43e27\',
    \'query_vars_changed\' => false,
    \'thumbnails_cached\' => false,
    \'stopwords\' => NULL,
    \'compat_fields\' => array(
        0 => \'query_vars_hash\',
        1 => \'query_vars_changed\',
    ),
    \'compat_methods\' => array(
        0 => \'init_query_flags\',
        1 => \'parse_tax_query\',
    ),
));

var_export result without country.

WP_Query::__set_state( array(
    \'query\' => array(
        \'s\' => \'\',
        \'post_type\' => \'auto-listing\',
        \'model\' => array(
            0 => \'F12 Berlinetta\',
            1 => \'California\',
        ),
        \'condition\' => array(
            0 => \'New\',
            1 => \'Used\',
        ),
        \'min_price\' => \'\',
        \'max_price\' => \'\',
        \'odometer\' => \'\',
        \'within\' => \'\',
    ),
    \'query_vars\' => array (
        \'s\' => \'\',
        \'post_type\' => \'auto-listing\',
        \'model\' => array(
            0 => \'F12 Berlinetta\',
            1 => \'California\',
        ),
        \'condition\' => array(
            0 => \'New\',
            1 => \'Used\',
        ),
        \'min_price\' => \'\',
        \'max_price\' => \'\',
        \'odometer\' => \'\',
        \'within\' => \'\',
        \'error\' => \'\',
        \'m\' => \'\',
        \'p\' => 0,
        \'post_parent\' => \'\',
        \'subpost\' => \'\',
        \'subpost_id\' => \'\',
        \'attachment\' => \'\',
        \'attachment_id\' => 0,
        \'name\' => \'\',
        \'static\' => \'\',
        \'pagename\' => \'\',
        \'page_id\' => 0,
        \'second\' => \'\',
        \'minute\' => \'\',
        \'hour\' => \'\',
        \'day\' => 0,
        \'monthnum\' => 0,
        \'year\' => 0,
        \'w\' => 0,
        \'category_name\' => \'\',
        \'tag\' => \'\',
        \'cat\' => \'\',
        \'tag_id\' => \'\',
        \'author\' => \'\',
        \'author_name\' => \'\',
        \'feed\' => \'\',
        \'tb\' => \'\',
        \'paged\' => 0,
        \'meta_key\' => \'\',
        \'meta_value\' => \'\',
        \'preview\' => \'\',
        \'sentence\' => \'\',
        \'title\' => \'\',
        \'fields\' => \'\',
        \'menu_order\' => \'\',
        \'embed\' => \'\',
        \'category__in\' => array(),
        \'category__not_in\' => array(),
        \'category__and\' => array(),
        \'post__in\' => array(),
        \'post__not_in\' => array(),
        \'post_name__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(),
        \'post_status\' => \'publish\',
        \'orderby\' => \'date ID\',
        \'order\' => \'DESC\',
        \'posts_per_page\' => 10,
        \'meta_query\' => array(
            0 => array(
                0 => array(),
                1 => array(
                    \'key\' => \'_al_listing_model_name\',
                    \'value\' => array(
                        0 => \'F12 Berlinetta\',
                        1 => \'California\',
                    ),
                    \'compare\' => \'IN\',
                ),
                2 => array(
                    \'key\' => \'_al_listing_condition\',
                    \'value\' => array(
                        0 => \'New\',
                        1 => \'Used\',
                    ),
                    \'compare\' => \'IN\',
                ),
                3 => array(),
                4 => array(),
                \'relation\' => \'AND\',
            ),
        ),
        \'tax_query\' => NULL,
    ),
    \'tax_query\' => WP_Tax_Query::__set_state( array(
        \'queries\' => array(),
        \'relation\' => \'AND\',
        \'table_aliases\' => array(),
        \'queried_terms\' => array(),
        \'primary_table\' => NULL,
        \'primary_id_column\' => NULL,
    )),
    \'meta_query\' => false,
    \'date_query\' => false,
    \'post_count\' => 0,
    \'current_post\' => -1,
    \'in_the_loop\' => false,
    \'comment_count\' => 0,
    \'current_comment\' => -1,
    \'found_posts\' => 0,
    \'max_num_pages\' => 0,
    \'max_num_comment_pages\' => 0,
    \'is_single\' => false,
    \'is_preview\' => false,
    \'is_page\' => false,
    \'is_archive\' => true,
    \'is_date\' => false,
    \'is_year\' => false,
    \'is_month\' => false,
    \'is_day\' => false,
    \'is_time\' => false,
    \'is_author\' => false,
    \'is_category\' => false,
    \'is_tag\' => false,
    \'is_tax\' => false,
    \'is_search\' => true,
    \'is_feed\' => false,
    \'is_comment_feed\' => false,
    \'is_trackback\' => false,
    \'is_home\' => false,
    \'is_404\' => false,
    \'is_embed\' => false,
    \'is_paged\' => false,
    \'is_admin\' => false,
    \'is_attachment\' => false,
    \'is_singular\' => false,
    \'is_robots\' => false,
    \'is_posts_page\' => false,
    \'is_post_type_archive\' => true,
    \'query_vars_hash\' => \'53d40a59288cf391a5bf428c30c6ae21\',
    \'query_vars_changed\' => false,
    \'thumbnails_cached\' => false,
    \'stopwords\' => NULL,
    \'compat_fields\' => array(
        0 => \'query_vars_hash\',
        1 => \'query_vars_changed\',
    ),
    \'compat_methods\' => array(
        0 => \'init_query_flags\',
        1 => \'parse_tax_query\',
    ),
))

1 个回复
最合适的回答,由SO网友:kierzniak 整理而成

看看你的meta_query 参数。有无country 参数。

// With country
\'meta_query\' => array(
    0 => array(
        \'key\' => \'_al_listing_country\',
        \'value\' => array(
            0 => \'Pakistan\',
            1 => \'United Kingdom\',
        ),
        \'compare\' => \'IN\',
        0 => array(),
        1 => array(
            \'key\' => \'_al_listing_model_name\',
            \'value\' => array(
                0 => \'F12 Berlinetta\',
                1 => \'California\',
            ),
            \'compare\' => \'IN\',
        ),
        2 => array(
            \'key\' => \'_al_listing_condition\',
            \'value\' => array(
                0 => \'New\',
                1 => \'Used\',
            ),
            \'compare\' => \'IN\',
        ),
        3 => array(),
        4 => array(),
        \'relation\' => \'AND\',
    ),
),

// Without country
\'meta_query\' => array(
    0 => array(
        0 => array(),
        1 => array(
            \'key\' => \'_al_listing_model_name\',
            \'value\' => array(
                0 => \'F12 Berlinetta\',
                1 => \'California\',
            ),
            \'compare\' => \'IN\',
        ),
        2 => array(
            \'key\' => \'_al_listing_condition\',
            \'value\' => array(
                0 => \'New\',
                1 => \'Used\',
            ),
            \'compare\' => \'IN\',
        ),
        3 => array(),
        4 => array(),
        \'relation\' => \'AND\',
    ),
)
您的key, valuecompare 参数与其他参数不在同一嵌套级别上meta_query 过滤器。这就是问题所在。

您的国家/地区meta\\u查询筛选器不在数组中,正在以不同的方式与其他meta查询合并。您应该在中返回数组的数组country_query 方法

/**
 * Returns a meta query for filtering by country.
 * @return type
 */
private function country_query() {

    if ( isset( $_GET[\'country\'] ) && ! empty( $_GET[\'country\'] ) ) {

        $data = array_map( \'sanitize_text_field\', wp_unslash( $_GET[\'country\'] ) );

        return array(
            array(
                \'key\'     => \'_al_listing_country\', 
                \'value\'   => $data,
                \'compare\' => \'IN\'
            )
        );
    }

    return array();
}
或从中添加结果country_query 与其他筛选器一样作为数组项。

$country_query[] = $this->country_query();

结束

相关推荐

Error in database connection

我正在使用Wamp服务器,我想用WordPress开发网站。我已配置wp配置。php。它仍在向我展示建立数据库连接时出错。我应该如何解决此错误?