I’m using this function for customizing search results:

function order_search_by_posttype( $orderby ) {
if( ! is_admin() && is_search() ) :
    global $wpdb;
    $orderby =
        "CASE WHEN {$wpdb->prefix}posts.post_type = 'page' THEN '1' 
             WHEN {$wpdb->prefix}posts.post_type = 'directory' THEN '2' 
             WHEN {$wpdb->prefix}posts.post_type = 'post' THEN '3' 
        ELSE {$wpdb->prefix}posts.post_type END ASC, 
        {$wpdb->prefix}posts.post_title ASC";
endif;
return $orderby;  }

Is there a way to show a definite page first and then the rest using $orderby?
I tried to do the folllowing, but it didn’t work:

function order_search_by_posttype( $orderby ){
if( ! is_admin() && is_search() ) :
    global $wpdb;
    $orderby =
        "CASE WHEN {$wpdb->prefix}posts.post__in  => array(6560,6561) THEN '1'
              WHEN {$wpdb->prefix}posts.post_type = 'page' THEN '2' 
              WHEN {$wpdb->prefix}posts.post_type = 'directory' THEN '3' 
              WHEN {$wpdb->prefix}posts.post_type = 'post' THEN '4' 
        ELSE {$wpdb->prefix}posts.post_type END ASC, 
        {$wpdb->prefix}posts.post_title ASC";
endif;
return $orderby;}

Read more here: Search results posts_orderby and ID


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: