I created a search filter that displays posts randomly, and I wonder how can I always display a specific post item in the first position of the search results and display the rest of the items normally.
$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) : '';
}
I created a search filter that displays posts randomly, and I wonder how can I always display a specific post item in the first position of the search results and display the rest of the items normally.
$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) : '';
}
Share
Improve this question
edited Feb 15, 2019 at 13:11
Pratik Patel
1,1091 gold badge11 silver badges23 bronze badges
asked Feb 15, 2019 at 13:08
Frenchy_WPFrenchy_WP
191 silver badge6 bronze badges
1 Answer
Reset to default 0One way to show a specific post always first is to add a sorting loop between your post query and rendering loop.
For example,
$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