1

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;}
  • Changed this to: CASE WHEN {$wpdb->prefix}posts.ID IN (6560) THEN '1', though the result still not on the first page – Natalya May 23 '17 at 18:53

Your Answer

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

Browse other questions tagged or ask your own question.