Support

Account

Home Forums Backend Issues (wp-admin) Retrieving Relationship Values in Admin Columns – Fetching the ID, not Name

Solved

Retrieving Relationship Values in Admin Columns – Fetching the ID, not Name

  • Hello there,

    I’m adding some extra columns to the Admin section for a particular CPT. I want to list the value of a Relationship field in a column.

    I’ve followed the instructions and am getting the column and the value of the field location_assign_client (a Relationship field) for each of those posts. That’s great, however, it is listing the ID of related post, not the name of it.

    Here’s my code:

    
    function my_page_columns($columns)
    {
    	$columns = array(
    	'cb'		=> '<input type="checkbox" />',
    	'title'		=> 'Title',
    	'client'	=> 'Client',
    	'date'		=> 'Date',
    	'author'	=> 'Author',
    	);
    	return $columns;
    }
    
    function my_custom_columns($column)
    {
    	global $post;
    	if($column == 'client')
    	{
    		if(get_field('location_assign_client'))
    		{
    			echo get_field('location_assign_client');
    		}
    	}
    }
    
    add_action("manage_location_posts_custom_column", "my_custom_columns");
    add_action("manage_location_posts_columns", "my_page_columns");
    
    function my_column_register_sortable( $columns )
    {
    	$columns['client'] = 'client';
    	return $columns;
    	add_filter("manage_location_posts_sortable_columns", "my_column_register_sortable");
    }
    
  • When you set up a taxonomy field you have the choice to return the Term ID or the Term Object. Either way you need to do a little more work to show the term name.

    
    // returning Term ID
    $term_id = get_field('location_assign_client');
    // https://codex.wordpress.org/Function_Reference/get_term_by
    $term = get_term_by('id', $term_id, $YOUR_TAXONOMY);
    echo $term->name;
    

    OR

    
    // returning Term Object
    $term = get_field('location_assign_client');
    echo $term->name;
    
  • Hi @hube2 thanks for the quick reply, most grateful.

    Sadly, that just returns blank. My current code works with it set as ID, so I set that to Object and changed the code to what you have there. And that returns blank.

    $term->name; makes complete sense but I don’t understand why that is returning blank.

  • This could be because a taxonomy field can hold an array or terms. You may need to do something like echo $term[0]->name;

    Using the ID example

    
    $terms = get_field('location_assign_client');
    $term = get_term_by('id', $term[0], $YOUR_TAXONOMY);
    
  • Hey John, thanks again for the quick reply.

    I have tried this again sadly it’s blank.

    As you mentioned taxonomies, to confirm, this is a Relationship field, the relationship being between posts. So, in this instance, the Add New Location (a cpt) admin page shows a column, which shows how that Location CPT is assigned to a Client CPT. It’s been a long day so perhaps I’ve misunderstood ha, but we’re not using taxonomies in this instance.

    The field will only take one value.

    Sorry if that wasn’t clearer earlier, hopefully that extra bit of info might help this along.

    Thanks!

  • I misunderstood, I thought it was a taxonomy field

    
    function my_custom_columns($column)
    {
    	global $post;
    	if($column == 'client')
    	{
    		if(get_field('location_assign_client'))
    		{
    			echo get_the_title(get_field('location_assign_client'));
    		}
    	}
    }
    
  • Waheyy! Thanks @hube2, that’s perfect! I didn’t even consider that get_the_title would work.

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

The topic ‘Retrieving Relationship Values in Admin Columns – Fetching the ID, not Name’ is closed to new replies.