为什么我的可湿性粉剂元查询没有返回任何结果?

时间:2013-09-05 作者:kuchikoo

接受$\\u GET中的值作为WP元查询中的键不合适吗?我不知道为什么这段代码不返回任何结果。。。

对价格和卧室的查询都不会返回任何结果。我的代码是否不正确?任何帮助都将不胜感激。。

<?php
if ( get_query_var(\'paged\') ) {
$paged = get_query_var(\'paged\');
} else if ( get_query_var(\'page\') ) {
$paged = get_query_var(\'page\');
} else {
$paged = 1;
}
$args = array(
\'post_type\' => \'property\',
\'paged\' => $paged
);

if($_GET[\'beds\'] >= 1) {
$args[\'meta_query\'][] = array(
\'key\' => $_GET[\'beds\'],
\'value\' => array(\'pyre_BHK-A\',\'pyre_BHK-B\',\'pyre_BHK-C\',\'pyre_BHK-D\',\'pyre_BHK-E\'),
\'compare\' => \'IN\'
);
}

if(($_GET[\'price-max\'] >= 1)&&($_GET[\'price-min\'] >= 1)) {
$args[\'meta_query\'][] = array(
\'relation\' => \'OR\',
array(
\'key\' => \'pyre_price\',
\'value\' => array($_GET[\'price-min\'],$_GET[\'price-max\']),
\'compare\' => \'BETWEEN\',
\'type\' => \'numeric\'
),
array(
\'key\' => \'pyre_pricem\',
\'value\' => array($_GET[\'price-min\'],$_GET[\'price-max\']),
\'compare\' => \'BETWEEN\',
\'type\' => \'numeric\'
)
);
}

query_posts($args);
if(have_posts()):
?>

<?php
while(have_posts()): the_post();
?>


<?php get_template_part( \'property-listing\' );           // Navigation bar (property-listing.php) ?>

<?php endwhile; ?>

<?php else: ?>
<h3><?php echo of_get_option(\'search_results_none_title\', \'No properties were found which match your search criteria.\'); ?></h3>
<p><?php echo of_get_option(\'search_results_none_content\', \'Try broadening your search to find more results.\'); ?></p>
<?php endif; ?>

1 个回复
SO网友:gmazzap

您在如何操作上有一些错误meta_query 为设置数组price-maxprice-min.

如果没有,代码中也会出现问题$_GET[\'beds\'] 已发送,但$_GET[\'price-min\']$_GET[\'price-max\'] 是这是因为$args[\'meta_query\'] 数组设置在内部if($_GET[\'beds\'] >= 1).

此外,如果GET 未发送,您必须检查$_GET 变量是在使用之前设置的,此外,如果要用作数字,则应确保它们是数值。

最后query_posts 应始终避免使用WP_Query 相反

此代码应该可以工作,但untested:

<?php
if ( ! is_front_page() ) {
  $paged = get_query_var(\'paged\') ? : 1;
} else {
  $paged = get_query_var(\'page\') ? : 1;
}

$beds = isset($_GET[\'beds\']) && intval($_GET[\'beds\']) ? $_GET[\'beds\'] : 0;
$max = isset($_GET[\'price-max\']) && intval($_GET[\'price-max\']) ? $_GET[\'price-max\'] : 0;
$min = isset($_GET[\'price-min\']) && intval($_GET[\'price-min\']) ? $_GET[\'price-min\'] : 0;

$args = array(
  \'post_type\' => \'property\',
  \'paged\' => $paged
);

if( $beds >= 1) {
  $args[\'meta_query\'][] = array(
    \'key\' => $beds, // are you sure for that?
    \'value\' => array(\'pyre_BHK-A\',\'pyre_BHK-B\',\'pyre_BHK-C\',\'pyre_BHK-D\',\'pyre_BHK-E\'),
    \'compare\' => \'IN\'
  );
}

if( ($max >= 1) && ($min) ) {
  if ( ! isset($args[\'meta_query\']) ) $args[\'meta_query\'] = array();
  $args[\'meta_query\'][\'relation\'] = \'OR\';
  $args[\'meta_query\'][] = array(
    \'key\' => \'pyre_price\',
    \'value\' => array($min, $max),
    \'compare\' => \'BETWEEN\',
    \'type\' => \'numeric\'
  );
  $args[\'meta_query\'][] = array(
    \'key\' => \'pyre_pricem\',
    \'value\' => array($min, $max),
    \'compare\' => \'BETWEEN\',
    \'type\' => \'numeric\'
  );
}

$query = new WP_Query($args);
if( $query->have_posts() ) : while( $query->have_posts() ): $query->the_post();

get_template_part( \'property-listing\' );
// Navigation bar (property-listing.php)

endwhile;

else:
?>  
<h3><?php echo of_get_option(\'search_results_none_title\', \'No properties were found which match your search criteria.\'); ?></h3>
<p><?php echo of_get_option(\'search_results_none_content\', \'Try broadening your search to find more results.\'); ?></p>
<?php
wp_reset_postdata();
endif;
?>

结束

相关推荐

使用WP自定义页面模板的jQuery

我有一个简单的jQuery脚本来启用图像鼠标悬停效果,我想在我的WP自定义页面模板上使用它,但它还不适合我,我假设它没有链接到jQuery。不会显示悬停图像。这是页面模板代码的一部分(主题=Twenty\\u-Eleven)<?php /** * Template Name: rpdcustompagetemplateportfolio * * @package WordPress * @subpackage Twenty_Eleven *