Hi,
I have a custom post type called ‘reserveringen’. Inside this post type I have a custom field called ‘patient’ which is a object field to wordpress users. It returns a user_id. To show the user last_name in this post types admin columns I have this:
// Add custom admin columns
function columns_reserveringen($columns) {
unset($columns['date']);
$columns['achternaam'] = 'Achternaam';
$columns['voornaam'] = 'Voornaam';
$columns['status'] = 'Status';
$columns['datums'] = 'Datum';
return $columns;
}
add_filter('manage_reserveringen_posts_columns', 'columns_reserveringen');
// Make 'patient' column sortable
function make_patient_column_sortable($columns) {
$columns['achternaam'] = 'achternaam';
return $columns;
}
add_filter('manage_edit-reserveringen_sortable_columns', 'make_patient_column_sortable');
add_action( 'pre_get_posts', 'custom_patient_column_sorting' );
function custom_patient_column_sorting( $query ) {
if( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'achternaam' === $query->get( 'orderby') ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'patient' );
}
}
function display_posts_stickiness( $column, $post_id ) {
if ($column == 'achternaam') {
$patient = get_field('patient', $post_id);
$last_name = get_user_meta( $patient, 'last_name', true );
echo $last_name;
}
}
add_action( 'manage_posts_custom_column' , 'display_posts_stickiness', 10, 2 );
But now it doesn’t order by the user last_name field.
How can I manage that it does?
If it shorting then it is sorting by the user ID. This is what ACF stores and it is not possible to sort by a value associated with a user.
You can only sort posts by data that is directly associated with the post. To do this the user’s name would have to be copied to a meta value for every post that user is related to.
You must be logged in to reply to this topic.
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.