• Hi everyone, I was working on the search result and got some answers from the support, so I add the code inside the function.php which is working fine, but the search result is kind of odd.

    Here’s the code:

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

    1. How do I hide homepage on the search result?
    2. My first search result list only had a title no excerpt. why?
    3. Can this reverse? Will this really affect WordPress DB? if I take the code out will it back to original?

    Thank you so much for the help.

Viewing 11 replies - 1 through 11 (of 11 total)
  • 1. Yes, we can remove the homepage from the search like this:

    function exclude_home_from_search($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search) {
          $query->set('post__not_in', array('home_page_id'));
        }
      }
    }
    
    add_action('pre_get_posts','exclude_home_from_search',11,1);

    In place of home_page_id please write the ID of the homepage.

    2. It depends on the search page template your theme providing, they must have not included the excerpt. You can check in their demo once.

    3. Yes, everything will be back to original if you will remove the code.

    Thanks

    Thread Starter madebymt

    (@madebymt)

    @prashantvatsh

    Thank you so much for answers my question.
    Is possible add that function in the WordPress SQL CASE Expression function I had?
    I not quite sure how I add it in.

    Thanks!

    • This reply was modified 7 years, 8 months ago by madebymt.
    Thread Starter madebymt

    (@madebymt)

    Tried to add this after if statement, but it’s not working

    Thanks!

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

    Thanks!

    • This reply was modified 7 years, 8 months ago by madebymt.
    • This reply was modified 7 years, 8 months ago by madebymt.

    $wp_query->set( 'post__not_in', array( pade_id ) );

    I have asked to write page id and that’s integer id, you have to go in dashboard and click on edit page(obviously for front page only) then in URL you can see that id and also write it in single quotes like this:

    $wp_query->set( 'post__not_in', array( 'your page id here') );

    If you are still not able to figure out the page id then please activate this plugin https://ww.wp.xz.cn/plugins/reveal-ids-for-wp-admin-25/ and go to pages in dashboard and check what’s the ID there on frontpage.

    Thanks

    Thread Starter madebymt

    (@madebymt)

    Hi @prashantvatsh
    I tried to put the code in the function, got the post id form the backend, but it’s still not working.
    Did I miss something?

    $wp_query->set( 'post__not_in', array( '20') );

    Thank you so much!

    function exclude_home_from_search($query) {
      if ( !is_admin() && $query->is_main_query() ) {
        if ($query->is_search) {
          $query->set('post__not_in', array('20'));
        }
      }
    }
    
    add_action('pre_get_posts','exclude_home_from_search',9999,1);

    Can you paste this directly, instead of pasting a part in that function, please.

    Thanks

    Thread Starter madebymt

    (@madebymt)

    @prashantvatsh

    Thank you so much for answer my question and be so patient with me.
    Is possible make your function to make search result show page first then post? That because I’m use SQL CASE expression.

    Thank you again.

    Thread Starter madebymt

    (@madebymt)

    If anyone has any ideas, please help! Thank you all.

    Hi,

    No, because both have different hooks and both have to return different results. So you have to use them separately.

    One of the simple solution to get pages first will be this: https://wordpress.stackexchange.com/questions/41691/how-do-i-filter-the-search-results-order

    Thanks

    Thread Starter madebymt

    (@madebymt)

    @prashantvatsh

    Thank you so much for your help, seriously!
    I put your function before the DB search filter, works perfectly.

    Thank you so much taking your time answering my questions.

    Pleasure is all mine šŸ™‚

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Reverse wordpress SQL CASE Expression’ is closed to new replies.