All Topics
Custom orderby for Sort, Filter of ACF post_object field
Hi, I have a CPT “Artworks” which is related to a CPT “Artist” using an ACF post object field. Right now the Artist column returns the title (which is first_name last_name) and therefore alphabetizes by the first names. Is there a way to use the ac/column/value
filter so that I can replace the post object field value for Sorting and Filtering so that it alphabetizes by the artist’s custom fields last_name and first_name? I’m using this code but it doesn’t work (and I think that it would actually change how it’s displayed in the list, not the Sort/Filter).
function ac_my_acf_column_value_example( $value, $id, $column ) {
if ( $column instanceof ACF ) {
$meta_key = $column->get_meta_key(); // This gets the ACF field key
$meta_value = $column->get_meta_value(); // This gets the ACF field key
$acf_field = $column->get_acf_field(); // Gets an ACF object
if( 'artists_name' == $acf_field ){
// Alter the display $value
$value = get_field('artist_last_name', $meta_value);
}
}
return $value;
}
add_filter( 'ac/column/value', 'ac_my_acf_column_value_example', 10, 3 );
You must be logged in to reply to this topic.