不知道我在这里做错了什么。
我有一个用于搜索属性的搜索功能,可用于搜索自定义字段。我想使用搜索结果顶部的下拉过滤器按价格asc和desc订购搜索结果。
我有一个search.php,我把这个代码放在其中:
<?php include (TEMPLATEPATH . '/search-query.php'); ?>
<form name="formorder" method="post">
<select name="resultsorder" onChange="formorder.submit();">
<option>Order Results By</option>
<option value="Price High-Low" <?php echo ($resultsorder == 'Price High-Low')? 'selected="selected"':''; ?>>Price Low-High</option>
<option value="Price High-Low" <?php echo ($resultsorder == 'Price High-Low')? 'selected="selected"':''; ?>>Price High-Low</option>
<option value="a-z" <?php echo ($resultsorder == 'a-z')? 'selected="selected"':''; ?>>a-z</option>
</select>
</form>
And I have a search-query.php containing this code:
<?php if($resultsorder) {
//get value from order dropdown on search results page
$resultsorder = $resultsorder;
} else {
$resultsorder = get_option('wp_searchorder');}
switch ($resultsorder) {
case "Price High-Low":
$metakey = 'price';
$order = 'DESC';
$orderby = 'meta_value_num';
break;
case "Price Low-High":
$metakey = 'price';
$order = 'ASC';
$orderby = 'meta_value_num';
break;
case "a-z":
$metakey = 'address';
$order = 'ASC';
$orderby = 'meta_value';
break;}
if (!empty($_ids) && !$alllistings) {
$wpq = array ('post_type' => 'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => $_ids, 'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => 9999 );
} elseif (empty($_ids) && !$alllistings) {
// $_ids array is empty because search got no results
// $_ids array will be empty if page is an "All Listings" page. Don't run this code if is All Listings because All Listings will show all listings. This code will display "no results found"
$wpq = array ('post_type' =>'listing', 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post__in' => array('0'),'post_status' => 'publish', 'paged' => $paged, 'posts_per_page' => 9999);
} elseif ($alllistings) {
// This is an All Listings page, so show all results
$wpq = array ('post_type' =>'listing', 'paged' => $paged, 'meta_key' => $metakey, 'orderby' => $orderby, 'order' => $order, 'post_status' => 'publish', 'posts_per_page' => 9999);
}
$listing = new WP_Query($wpq);?>
不确定我做错了什么。
答案 0 :(得分:0)
你必须向我们提供更多细节。你收到错误了吗?如果没有,你期待看到什么,你实际看到了什么?
一个可能的问题是即使他们选择“价格低 - 高”,你也会通过“价格高 - 低”。这一行:
<option value="Price High-Low" <?php echo ($resultsorder == 'Price High-Low')? 'selected="selected"':''; ?>>Price Low-High</option>
错了。
修改强>
好的,我创建了一个简单的主题,复制了你的样子。在functions.php
我有:
<?php
add_action('init', 'create_listing_post_type');
function create_listing_post_type() {
register_post_type('listing', array(
'labels' => array(
'name' => 'Listings',
'singular_name' => 'Listing',
'add_new_item' => 'Add a listing',
'edit_item' => 'Edit listing',
'new_item' => 'Add a listing',
'search_items' => 'Find a listing',
'not_found' => 'No listing found',
'not_found_in_trash' => 'No listing found in the trash'
),
'public' => true,
'supports' => array('title', 'editor', 'custom-fields')
));
}
?>
我进入管理界屏幕并添加了三个列表:
然后我根据您的代码在showlistings.php
添加了一个新模板:
<?php
/**
* Template Name: showlistings
*/
get_header();
//$resultsorder = 'Price High-Low';
//$resultsorder = 'Price Low-High';
$resultsorder = 'a-z';
switch ($resultsorder) {
case "Price High-Low":
$metakey = 'price';
$order = 'DESC';
$orderby = 'meta_value_num';
break;
case "Price Low-High":
$metakey = 'price';
$order = 'ASC';
$orderby = 'meta_value_num';
break;
case "a-z":
$metakey = 'address';
$order = 'ASC';
$orderby = 'meta_value';
break;
}
$wpq = array (
'post_type' =>'listing',
'meta_key' => $metakey,
'orderby' => $orderby,
'order' => $order);
$listings = new WP_Query($wpq);
foreach( $listings->posts as $listing) {
echo $listing->post_title;
echo '<br />';
}
get_footer(); ?>
好消息是你的代码对我有用:当我更改$resultsorder
的值(更改注释掉的行)时,我得到了预期的结果。
因此,需要检查一些事项:
$resultsorder
的值是否按预期设置(即是从表单提交,是否从$_POST
变量填充?将echo "\$resultsorder = $resultsorder";
放在输出的某处检查。price
值是否以MySQL可以将其解析为数字的方式存储?例如,“10000”,而不是(比如说)“10,000美元”。 WordPress有MySQL通过向它们添加0将数字转换为字符串 - 如果它们不能被解析为数字,它们都将返回0并且任何排序都将毫无意义。$alllistings
和$_ids
变量是否按预期填充(即,if
- then
逻辑的预期分支是否正在执行?再次使用{{1 (或echo
)将帮助您检查值。我打破你的调查。检查表单提交是否正常,然后正确设置查询参数,然后结果符合预期。