0

Hi the default wordpress search seems to be for "piano"


SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
AND (((wp_posts.post_title LIKE '%piano%')
OR (wp_posts.post_excerpt LIKE '%piano%')
OR (wp_posts.post_content LIKE '%piano%')))
AND wp_posts.post_type IN ('post', 'page', 'attachment')
AND (wp_posts.post_status = 'publish'
OR wp_posts.post_author = 5
AND wp_posts.post_status = 'private')
ORDER BY wp_posts.post_title LIKE '%piano%' DESC, wp_posts.post_date DESC
LIMIT 0, 25

however I need them all sorted by date first. My search results by date are skewed:

2020-10-25  
2020-10-22  
2020-10-20  
2020-10-20  
2020-10-16  
2020-10-13  
2020-10-10  
2020-10-07  
2020-10-06  
2020-10-02  
2020-09-30  
2020-09-28  
2020-09-02  
2020-08-09  
2020-07-21  
2020-07-21  
2020-05-21  
2020-04-24  
2020-03-06  
2019-06-02  <- here it is skewed onwards
2019-06-01  
2020-10-25  
2020-10-24  
2020-10-23  
2020-10-23

So I asume it is because the SQL query sorts by title first.

So I tried this solution:


function changeSearchSort( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_search()) {
        $orderby =  $wpdb->prefix."posts.post_date ASC";
    }
    return  $orderby;
}
add_filter('posts_orderby','changeSearchSort',10,2);

from here How do I change WP search results order?

and it does not work.

I have tried this:
a) disabled all plugins
b) different themes

still not working.

Help appreciated

cheers

Ben

2
  • Where did you put your code - in the theme functions file?
    – Sally CJ
    Commented Oct 29, 2020 at 14:34
  • Hi Sally, in my own plugin to act like a functions file...which I didnt disable but all others I did. I found a workaround to add this function my_archive_query_override( $query ) { if ( $query->is_main_query() ) { $query->set( 'orderby', 'DESC' ); } } add_action( 'pre_get_posts', 'my_archive_query_override' ); ``` however it only adds the SQL query AFTER the one I shown above, so it doesnt alter the original wordpress query Commented Nov 1, 2020 at 4:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.