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 badge1 Answer
Reset to default 1You 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);