Created
December 24, 2022 11:13
-
-
Save barrd/b2e5218cf45013e696f029f618d9aec4 to your computer and use it in GitHub Desktop.
WordPress custom search order by date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Custom search order by Date | |
* | |
* @param string $orderby Orderby string. | |
* @param object $query The query. | |
*/ | |
function barrd_sort_search_by_date( $orderby, $query ) { | |
global $wpdb; | |
if ( ! is_admin() && is_search() ) { | |
$orderby = "{$wpdb->prefix}posts.post_type ASC, {$wpdb->prefix}posts.post_date DESC"; | |
} | |
return $orderby; | |
} | |
add_filter( 'posts_orderby', 'barrd_sort_search_by_date', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment