最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I change WP search results order?

programmeradmin2浏览0评论

By default WP sorts newest Posts at the top of search results.

I’m using WP Posts as a Members list for a sewing referral group. The search results need to be in order of seniority, so the oldest posts need to appear at the top of the search results—the opposite of the default sort order.

I also have a Custom Field for member numbers, so I could easily use that instead of the post date for search result order.

I'm using a child theme, have no problem adding code to the functions.php file. I am NOT a coder so don't know how to do this on my own.

Thank you!

By default WP sorts newest Posts at the top of search results.

I’m using WP Posts as a Members list for a sewing referral group. The search results need to be in order of seniority, so the oldest posts need to appear at the top of the search results—the opposite of the default sort order.

I also have a Custom Field for member numbers, so I could easily use that instead of the post date for search result order.

I'm using a child theme, have no problem adding code to the functions.php file. I am NOT a coder so don't know how to do this on my own.

Thank you!

Share Improve this question edited Jun 4, 2019 at 14:15 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jun 4, 2019 at 14:15 Deb TDeb T 111 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 1

You can use the action hook posts_orderby in your child themes functions.php:

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);
发布评论

评论列表(0)

  1. 暂无评论