我有一个简单的下拉列表:
<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.search=this.options[this.selectedIndex].value;">
<option disabled selected >Select option</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'date\' && isset($_GET["order"]) && trim($_GET["order"]) == \'DESC\' ){ echo \'selected\'; } ?> value="?orderby=date&order=DESC">Newest</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'date\' && isset($_GET["order"]) && trim($_GET["order"]) == \'ASC\' ){ echo \'selected\'; } ?> value="?orderby=date&order=ASC">Oldest</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'title\' && isset($_GET["order"]) && trim($_GET["order"]) == \'ASC\' ){ echo \'selected\'; } ?> value="?orderby=title&order=ASC">A-Z Asc</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'title\' && isset($_GET["order"]) && trim($_GET["order"]) == \'DESC\' ){ echo \'selected\'; } ?> value="?orderby=title&order=DESC">A-Z Desc</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'50\' && isset($_GET["order"]) && trim($_GET["order"]) == \'ASC\' ){ echo \'selected\'; } ?> value="?meta_key=budget&orderby=50&order=ASC">Price Under $50</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'199\' && isset($_GET["order"]) && trim($_GET["order"]) == \'ASC\' ){ echo \'selected\'; } ?> value="?meta_key=budget&orderby=meta_value&meta_key=199&order=ASC">Price $50-199</option>
<option <?php if( isset($_GET["orderby"]) && trim($_GET["orderby"]) == \'200\' && isset($_GET["order"]) && trim($_GET["order"]) == \'ASC\' ){ echo \'selected\'; } ?> value="?meta_key=budget&orderby=200&order=ASC">Price Over $200</option>
</select>
和WP\\U查询:
$search_result = get_field(\'search_results\', \'option\', false);
$args4 = array(
\'post_type\' => array(\'post\', \'tools\', \'recipes\', \'page\'),
\'post__in\' => $search_result,
\'orderby\' => get_query_var(\'orderby\'),
\'posts_per_page\' => \'12\',
\'post_status\' => \'publish\',
);
我试图让查询在选中时按单个值排序,但之后仍显示其他帖子。
例如,任何带有自定义字段(复选框)选择“50”的帖子都会首先按字母顺序显示所有这些帖子,然后再按字母顺序显示其余帖子。
同样,如果选择了199的下拉列表,则显示的第一个帖子将是那些在“自定义字段”复选框中选择了199的帖子,然后剩余的帖子将按字母顺序显示在后面。
我在这里看到了很多帖子,但它们似乎都是在WP\\u查询参数中进行过滤的,但如果可能的话,我希望通过URL字符串进行过滤(因为我已经有了下拉设置并正在处理日期/标题)。
我尝试过:
?meta_key=budget&orderby=50&order=ASC
以及
?meta_key=budget&orderby=meta_value&key=50&order=ASC
谢谢!
编辑:我也尝试过:
function my_pre_get_posts( $wp_query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $wp_query;
}
// allow the url to alter the query
if( isset($_GET[\'budget\']) ) {
$wp_query->set(\'orderby\', \'meta_value_num\');
$wp_query->set(\'meta_key\', \'budget\');
$wp_query->set(\'meta_value\', $_GET[\'budget\']);
}
// return
return $wp_query;
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');
并将字符串设置为:
?budget=199&order=ASC
也不起作用:(