Home › Forums › General Issues › Ordering by custom field of relationship field object › Reply To: Ordering by custom field of relationship field object
Meanwhile, I’ve converted the Relationship field to a Post Object field since each Artwork only has one Artist associated with it. That has this post object query filter:
function artist_post_object_query( $args, $field, $post_id ) {
$args['meta_query'] = array(
'relation' => 'AND',
'last_name' => array(
'key' => 'artist_last_name'
),
'first_name' => array(
'key' => 'artist_first_name'
));
$args['orderby'] = array(
'last_name' => 'ASC',
'first_name' => 'ASC'
);
// return
return $args;
}
add_filter('acf/fields/post_object/query/name=artists_name', 'artist_post_object_query', 10, 3);
So Artist correctly alphabetized in the Artwork edit screen. (Although that filter doesn’t extend to the List screen sort order, for which I’m using Admin Columns Pro.)
Essentially I want that filter to work any time the Artworks field ‘artist_name’ is being used as a meta_key to sort so that ‘meta_value’ can be replaced with ‘artist_last_name’ + ‘artist_first_name’ instead of the post ID. It seems like a posts_join
and then posts_orderby
filter would work but I can’t get this join to work:
add_filter('posts_join', 'artist_join', 10, 2 );
function artist_join($joins)
{
if(is_post_type_archive('artworks') || (is_admin() && $_GET['post_type'] == 'artworks')) {
global $wpdb;
$joins .= " INNER JOIN {$wpdb->postmeta} pm_last_name ON pm_last_name.post_id={$wpdb->posts}.ID
WHERE pm_last_name.meta_key='artist_last_name'" ;
$joins .= " INNER JOIN {$wpdb->postmeta} pm_first_name ON pm_first_name.post_id={$wpdb->posts}.ID
WHERE pm_first_name.meta_key='artist_first_name'" ;
}
return $joins;
}
Any help would be much appreciated.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.