从两个下拉列表中获取自定义帖子类型查询有点困难。让我告诉你我在使用什么:
The Form:
<?php require($_SERVER[\'DOCUMENT_ROOT\'] . \'/wp-load.php\'); ?>
<form method="post" id="searchform" action="http://www.findabuilder247.com/directory">
<?php $args = array(
\'show_option_none\' => \'Select a Tradesman\',
\'name\' => \'catagory\',
\'post_type\' => \'profiles\',
\'hide_empty\' => \'1\'
); ?>
<?php wp_dropdown_categories( $args ); ?>
<?php $args = array(
\'show_option_none\' => \'Select Area\',
\'name\' => \'area\',
\'show_count\' => \'1\',
\'taxonomy\' => \'areas\',
\'orderby\' => \'name\',
\'post_type\' => \'profiles\',
\'hide_empty\' => \'1\'
); ?>
<?php wp_dropdown_categories( $args ); ?>
<input type="submit" value="" class="searchbutton4" />
</form>
然后,我将生成查询(或至少尝试使用):
<?php
if($_POST){
$query = $_POST;
$cats = array();
foreach($query as $key => $param) {
if($param != -1) {
array_push($cats, $param);
}
}
// print_r($cats); // This prints the array, so I know it\'s working
query_posts(array( \'category__and\' => $cats ));
} ?>
我正在使用一个自定义循环来最初调用查询,我希望将其用作搜索函数。
<?php $loop = new WP_Query( array( \'post_type\' => \'profiles\', \'posts_per_page\' => 10, \'order\'=> \'ASC\', \'orderby\' => \'title\' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
...
<?php endwhile; ?>
任何帮助都将不胜感激。我真是疯了!
UPDATE
詹姆斯·坎普是个绝对的大师!以下代码非常适合我:
<?php $loop = new WP_Query( array( \'post_type\' => \'profiles\', \'posts_per_page\' => 10, \'order\'=> \'ASC\', \'orderby\' => \'title\' ) ); ?>
<?php
if($_POST){
$query = $_POST;
$cats = array();
foreach($query as $key => $param) {
if($param != -1) {
array_push($cats, $param);
}
}
$args=array(
\'category__in\' => $cats,
\'post_type\' => \'profiles\',
\'posts_per_page\' => 10,
\'order\'=> \'ASC\',
\'orderby\' => \'title\'
);
$loop= null;
$loop = new WP_Query($args);
} ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
…
<?php endwhile; ?>
最合适的回答,由SO网友:James Kemp 整理而成
您似乎使用了两个单独的查询,第一个(query\\u posts)不会做任何事情,第二个是将要使用的查询。您是否尝试过:
$args=array(
\'category__in\' => $cats,
\'post_type\' => \'profiles\',
\'posts_per_page\' => 10,
\'order\'=> \'ASC\',
\'orderby\' => \'title\'
);
$loop= null;
$loop = new WP_Query($args);
尝试这样做,而不是:
query_posts(array( \'category__and\' => $cats ));
以及
<?php $loop = new WP_Query( array( \'post_type\' => \'profiles\', \'posts_per_page\' => 10, \'order\'=> \'ASC\', \'orderby\' => \'title\' ) ); ?>