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?