我创建了一个函数,用此代码过滤分类查询
add_action(\'pre_get_posts\', \'custom_taxonomy_query\');
$option_taxposts_per_page = get_option(\'tax_posts_per_page\');
function custom_taxonomy_query(&$query)
{
global $option_taxposts_per_page;
if (!is_admin() &&
is_tax(\'feature\') || is_tax(\'propertytype\') || is_tax(\'location\')) {
if (is_tax(\'feature\')) {
$browseorder = get_option(\'browse_feature_order\');
}
else if (is_tax(\'propertytype\')) {
$browseorder = get_option(\'browse_propertytype_order\');
}
else if (is_tax(\'location\')) {
$browseorder = get_option(\'browse_location_order\');
}
switch ($browseorder) {
case "Price Descending":
$metakey = \'_price\';
$order = \'DESC\';
$orderby = \'meta_value_num\';
break;
case "Price Ascending":
$metakey = \'_price\';
$order = \'ASC\';
$orderby = \'meta_value_num\';
break;
case "Date Descending":
$metakey = \'\';
$order = \'DESC\';
$orderby = \'date\';
break;
case "Date Ascending":
$metakey = \'\';
$order = \'ASC\';
$orderby = \'date\';
break;
case "Random":
$metakey = \'\';
$order = \'\';
$orderby = \'rand\';
break;
}
$query->set(\'meta_key\', $metakey);
$query->set(\'post_status\', \'publish\');
$query->set(\'orderby\', $orderby);
$query->set(\'order\', $order);
$query->set(\'posts_per_page\', $option_taxposts_per_page);
}
}
问题是如果我选择降价和升价的顺序。。我的自定义菜单导航消失了,我不知道这是什么原因。如果我选择按日期随机订购,一切都很好。请帮我解决这个问题。
最合适的回答,由SO网友:nonsensecreativity 整理而成
已解决!我使用了错误的技术来创建分类法的自定义查询,而不是使用上面的函数。我创建了使用query\\u vars的新函数。这是代码
function taxonomy_posts_order($query) {
global $browsetype;
$term = get_term_by( \'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );
if ( $query->query_vars[\'taxonomy\'] == $term->slug )
{
switch ($browsetype) {
case "Price Descending":
$metakey = \'_price\';
$order = \'DESC\';
$orderby = \'meta_value_num\';
break;
case "Price Ascending":
$metakey = \'_price\';
$order = \'ASC\';
$orderby = \'meta_value_num\';
break;
case "Date Descending":
$metakey = \'\';
$order = \'DESC\';
$orderby = \'date\';
break;
case "Date Ascending":
$metakey = \'\';
$order = \'ASC\';
$orderby = \'date\';
break;
case "Random":
$metakey = \'\';
$order = \'\';
$orderby = \'rand\';
break;
}
$query->query_vars[\'meta_key\'] = $metakey;
$query->query_vars[\'orderby\'] = $orderby;
$query->query_vars[\'order\'] = $order;
}
return $query;
}
if ( !is_admin() ) add_filter( \'pre_get_posts\', \'taxonomy_posts_order\' );