mina mo
mina mo

Reputation: 53

Sort Posts by 'ACF' custom field

I have wordpress website with enfold theme. i want to sort the post within a masonry based on an id that i added to the posts using Custom fields. for example the post titles are a , b , c. each one of them has an id. and they belong to category "cat" and the cat category is selected in masonry. now i want to sort the items in masonry by my custom id. this link

defines a function for customizing sort options. how can i add my meta key to this code? i also tried to use this but the key is not displaying in sort options.

Upvotes: 0

Views: 257

Answers (1)

John
John

Reputation: 5337

I did something similar recently, possibly this code will help you out. Add it to your functions.php

add_filter('posts_orderby','my_sort_custom',10,2);
function my_sort_custom( $orderby, $query ){
    global $wpdb;

    if(!is_admin() && is_category()) 
        $orderby =  $wpdb->prefix."postsmeta.MY_META_KEY ASC, {$wpdb->prefix}posts.post_date DESC";

    return  $orderby;
}

Upvotes: 1

Related Questions