我制作了一个带有选择框和文本字段的自定义搜索表单。
我有一个选择框,上面有“租赁”和“出售”(这是一个房地产网站)等选项。还有输入字段,用于最低和最高价格、位置等。
在帖子中,我为所有这些过滤器设置了自定义字段。
我用了WP_查询。它应该会起作用。以下是查询:
// Define the arguments for the WP query
$args = array(
\'post_type\' => \'post\',
\'relation\' => \'AND\',
\'meta_query\' => array(
array(
\'key\' => \'ex_lokacija\',
\'value\' => $grad ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_vrsta_oglasa\',
\'value\' => $adType ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_tip_nekretnine\',
\'value\' => $realEstateType ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_dio_pg\',
\'value\' => $dioGrada ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_dio_pg\',
\'value\' => $dioGrada ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'et_square_footage\',
\'value\' => array( $squareFrom, $squareTo ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\'
),
array(
\'key\' => \'et_price\',
\'value\' => array( $priceFrom, $priceTo ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\'
),
array(
\'key\' => \'et_bedrooms_number\',
\'value\' => $roomsNum,
\'type\' => \'numeric\',
\'compare\' => \'LIKE\'
)
)
);
$searched_posts = new WP_Query( $args );
我对每个$\\u GET vars使用preg\\u replace,并将它们分配给您在“value”中看到的变量。
现在,问题是它没有显示任何东西,即使应该有结果。
此外,如果没有“s”字段,它将无法工作-必须在其中添加一些内容,以便加载空的结果页。
Possible problem #1
我猜我创建的循环有一个问题,如下所示:
while ($searched_posts->have_posts()) : $searched_posts->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
这只是为了测试。
Possible problem #2
我把所有这些都放在一个文件中——搜索。php。也许这会引起问题?
这是:
\'\'第
global $wpdb;
$grad = preg_replace( \'/^[0-9a-zA-Z-]/\', \'\', $_GET[\'grad\'] );
$adType = preg_replace( \'/^[0-9a-zA-Z-]/\', \'\', $_GET[\'adType\'] );
$realEstateType = preg_replace( \'/^[0-9a-zA-Z-]/\', \'\', $_GET[\'realEstateType\'] );
$dioGrada = preg_replace( \'/^[0-9a-zA-Z-]/\', \'\', $_GET[\'dioGrada\'] );
$squareFrom = preg_replace( \'/[^0-9]/\', \'\', $_GET[\'squareFrom\'] );
$squareTo = preg_replace( \'/[^0-9]/\', \'\', $_GET[\'squareTo\'] );
$priceFrom = preg_replace( \'/[^0-9]/\', \'\', $_GET[\'priceFrom\'] );
$priceTo = preg_replace( \'/[^0-9]/\', \'\', $_GET[\'priceTo\'] );
$roomsNum = preg_replace( \'/[^0-9]/\', \'\', $_GET[\'roomsNum\'] );
// Change the defaults if not chosen
if($squareFrom == \'\') { $squareFrom = \'0\'; }
if($squareTo == \'\') { $squareTo = \'10000000\'; }
// Define the arguments for the WP query
$args = array(
\'post_type\' => \'post\',
\'relation\' => \'AND\',
\'meta_query\' => array(
array(
\'key\' => \'ex_lokacija\',
\'value\' => $grad ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_vrsta_oglasa\',
\'value\' => $adType ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_tip_nekretnine\',
\'value\' => $realEstateType ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_dio_pg\',
\'value\' => $dioGrada ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'ex_dio_pg\',
\'value\' => $dioGrada ,
\'compare\' => \'LIKE\'
),
array(
\'key\' => \'et_square_footage\',
\'value\' => array( $squareFrom, $squareTo ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\'
),
array(
\'key\' => \'et_price\',
\'value\' => array( $priceFrom, $priceTo ),
\'type\' => \'numeric\',
\'compare\' => \'BETWEEN\'
),
array(
\'key\' => \'et_bedrooms_number\',
\'value\' => $roomsNum,
\'type\' => \'numeric\',
\'compare\' => \'LIKE\'
)
)
);
$searched_posts = new WP_Query( $args );
get_header(); ?>
<div id="content-top">
<div id="menu-bg"></div>
<div id="top-index-overlay"></div>
<div id="content" class="clearfix">
<div id="main-area">
<?php get_template_part(\'includes/breadcrumbs\');
while ($searched_posts->have_posts()) : $searched_posts->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
</div> <!-- end #main-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
感谢您查看我的问题!