Support

Account

Home Forums ACF PRO Filter admin column on ACF Object field

Helping

Filter admin column on ACF Object field

  • 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.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.