Support » How-To and Troubleshooting » Query for Sorting Post by Post Meta Fields
4 years, 6 months ago
I have been stuck for a few hours on this problem and would love any suggestions on how to do this.
I have post meta field that is a checkbox with the name and value “featured”.
I need to order posts that have been modified or created in the last 24 hours and also have the post meta field “featured” first followed by the rest of the posts.
I have tried using the pre_get_posts filter but with no luck.
Thanks in advance.
Here is some sample code that I believe will do what you want:
Note that it sorts on the date published. If you want to sort on date modified, change this line:
$mam_global_orderby = "sort_key, $wpdb->posts.post_date DESC";
$mam_global_orderby = "sort_key, $wpdb->posts.post_modified DESC";
Thanks very much. That worked perfectly 🙂