我创建了一个随机显示帖子的搜索过滤器,我想知道如何始终在搜索结果的第一个位置显示特定的帖子项目,并正常显示其余项目。
$taxonomies = array(\'recipe_type\', \'product_category\', \'recipe_category\', \'recipe_event\', \'recipe_diet\');
foreach ($taxonomies as $taxonomy) {
$taxonomies_taxonomy[$taxonomy] = get_taxonomy($taxonomy);
$taxonomies_terms[$taxonomy] = get_terms(
array(
\'taxonomy\' => $taxonomy,
\'hide_empty\' => false,
\'meta_key\' => \'filter_order\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'ASC\'
)
);
$taxonomies_selected[$taxonomy] = (get_query_var($taxonomy)) ? get_query_var($taxonomy) : \'\';
}
最合适的回答,由SO网友:Antti Koskinen 整理而成
始终首先显示特定帖子的一种方法是在帖子查询和渲染循环之间添加排序循环。
例如
$my_posts = array(); // do your query first to get the posts
$sorted_posts = array(); // helper var to store sorted posts
// The sorting loop
foreach ( $my_posts as $post ) {
if ( "some logic here" ) {
array_unshift($sorted_posts, $post); // use php function to prepend the post to the sorted array
} else {
$sorted_posts[] = $post; // just push the posts to the end of the sorted array
}
}
// use $sorted_posts array in your rendering (foreach/while/for) loop