Support

Account

Home Forums General Issues Display ACF Post Object field in admin column

Solving

Display ACF Post Object field in admin column

  • Hello,

    I have a custom post type called “Film” which contains the “Post object” field..this field links each film to its director (which is another custom post type). I’m trying to display the director field in the admin column for film.

    I’m almost there, but having some issues displaying the content of the field..I believe I am not approaching the post object field in the right way.

    Here is my code in the functions.php :

    add_filter( 'manage_film_posts_columns', 'add_custom_column' );
    function add_custom_column( $columns ) {
        $columns['realisatrice'] = 'RĂ©alisatrice';
    
        return $columns;
    }
    
    // Add the data to the custom column
    
     $post_object = get_field('film_realisatrice');
    
                    if( $post_object ): 
    
                        // override $post
                        $post = $post_object;
                        setup_postdata( $post ); 
    
    add_action( 'manage_film_posts_custom_column' , 'add_custom_column_data', 10, 2 );
    function add_custom_column_data( $column, $post_id ) {
        switch ( $column ) {
            case 'realisatrice' :
                echo(the_field('film_realisatrice', $post_object->ID));
                break;
    			
        }
    }
    wp_reset_postdata(); 
    
               endif; 
    
    // Make column sortable 
    add_filter( 'manage_edit-film_sortable_columns', 'add_custom_column_make_sortable' );
    function add_custom_column_make_sortable( $columns ) {
    	$columns['realisatrice'] = 'RĂ©alisatrice';
    
    	return $columns;
    }
    
    // Add custom column sort request to post list page
    add_action( 'load-edit.php', 'add_custom_column_sort_request' );
    function add_custom_column_sort_request() {
    	add_filter( 'request', 'add_custom_column_do_sortable' );
    }
    
    // Column sorting
    function add_custom_column_do_sortable( $vars ) {
    
    	// check if post type is being viewed 
    	if ( isset( $vars['post_type'] ) && 'film' == $vars['post_type'] ) {
    
    		// check if sorting has been applied
    		if ( isset( $vars['orderby'] ) && 'realisatrice' == $vars['orderby'] ) {
    
    			// apply the sorting to the post list
    			$vars = array_merge(
    				$vars,
    				array(
    					'meta_key' => 'film_realisatrice',
    					'orderby' => 'meta_value_num'
    				)
    			);
    		}
    	}
    
    	return $vars;
    }

    Would strongly appreciate it if someone can point me in the right direction!

    thanks

  • Did you manage to do this?

    I’m trying do do something similar but I don’t know how to call a post_object field in functions.php

    Regards Pip

  • it depends on the return value for the post object field.

    If you are returning a post object then you can’t just echo the value, if you want, for example to display the post title

    
    $object = get_field('film_realisatrice', $post->id);
    echo get_the_title($object->ID);
    

    On the other hand if you are returning the id then you could do something like

    
    echo get_the_title(get_field('film_realisatrice', $post->ID));
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Display ACF Post Object field in admin column’ is closed to new replies.