Hi!
I try to order all post by a custom field called “jahr”. I tryed the following code within the function.php of my child theme:
add_filter('posts_join', 'add_join' );
function add_join($pjoin){
global $wpdb;
$pjoin .= "LEFT JOIN (
SELECT *
FROM $wpdb->postmeta
WHERE meta_key = 'jahr' ) AS postmeta ON $wpdb->posts.ID = postmeta.post_id";
return ($pjoin);
}
add_filter('posts_orderby', 'change_sortorder' );
function change_sortorder( $orderby ){
global $wpdb;
$orderby = "postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
return $orderby;
}
The mentioned code fits but also rss feed ist sorted by the custom field. Therefore new posts are not listed as “new post” in the rss feed when the custom field value is smaller then values of other posts (e.g. “1988” < “2016”). The “all posts” list in the backend is also affected by the custom post order.
How is it possible to have the desired result only visible for visitors and the standard order in the rss feed and the backend?
Thanks for your support!
BR, Patrick
Hey patrickmueller,
Unfortunately it would require quite some time and customization of the theme to achieve this, so I am sorry to tell you that this is not covered by our support. However if its really important for you to get this done, you can always hire a freelancer to do the job for you :)
Best regards,
Basilis
Ok, no problem.
Thanks!
BR, Patrick