0
votes

I have two different Custom Post Types, country and beach. For country, I just have a name field.

For beach, it's a little bit different, it has a field name and also a select with the countries that I added in my country custom type.

It's working.

Form to add a new beach connected to a country

Now I'm trying to create custom columns inside my beach custom type with:

Name | Country | Date

How to get the country name inside the beach posts?

My code now:

function add_custom_column_to_beaches($columns) {
  return array_merge ($columns, array(
    'country' => 'Country'
  ));
}

function country_custom_column ($column, $post_id) {
  switch ($column) {
    case 'country':
      echo get_post_meta($post_id, 'country_name', true);
      break;
  }
}

add_filter ('manage_beaches_posts_columns', 'add_custom_column_to_beaches' );
add_action ('manage_beaches_posts_custom_column', 'country_custom_column', 10, 2);

This is the result now:

Custom columns for advanced custom fields

I can't find how to retrieve the country name through the advanced custom fields. It always return a number. And when I tried to show with a print_r the variable, it doesn't have any country_name.

Thanks!

1
Is your id the same as the id belonging to the CPT item? (ie does Brazil in Countries have ID = 43)? - HdK
Good point! And yes, it is! I figure out on method from Advanced Custom Fields called get_field_object. It worked!! - Italo Borges

1 Answers

0
votes

Looking inside Advanced Custom Fields documentation I found a method that works with my approach.

function apply_custom_columns ($column) {
  global $post;
  switch ($column) {
    case 'country':
      $field = get_field_object('country_name', $post->ID);
      echo $field['value']->post_title;
      break;
  }
}

If someone has another work around, let me know!