您在如何操作上有一些错误meta_query
为设置数组price-max
和price-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;
?>