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(
        '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) : '';

One 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.