How to always display a specific post from the search result first

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

Leave a Comment