I'd like to re-order the search results based on post meta and date . The post meta key is _prioritize_s
and the value isn't important. So in a "normal" meta query, I'd just write the compare
parameters to check EXISTS/NOT EXISTS. While I have experience with meta queries, I've never had to re-order search results until now, so I'd appreciate any help.
This is how I'd like to re-order the search results:
- Check if posts with post meta
_prioritize_s
exists within search results - If they do, put those on top of the results in date order (from recent to oldest). After these posts are shown, show the remaining search results underneath in their default order.
- If there are no posts with this post meta among the search results, order by whatever is default
It looks like I have to use 2 filters, one to join and one to set the order. It seems like it's kind of working, but the order isn't right? Thoughts?
Updated Code from this sample
add_filter( 'posts_join', 'modify_search_results_join' );
add_filter( 'posts_orderby', 'modify_search_results_order', 10, 2 );
function modify_search_results_join( $orderby ) {
global $wpdb;
if ( ! is_admin() && is_search() ) {
$orderby .= "LEFT JOIN (
SELECT *
FROM $wpdb->postmeta
WHERE meta_key = '_prioritize_s' ) AS postmeta ON $wpdb->posts.ID = postmeta.post_id";
return $orderby;
}
return $orderby;
}
function modify_search_results_order( $orderby, $query ) {
global $wpdb;
//how do I order the results by _prioritize_s set to 'yes'?
$orderby = "postmeta.meta_value+'' DESC";
return $orderby;
}