Mike Cho Development

Post Object Field Using ACF: It’s Relational

Here’s the scenario: I am making a new custom post type for my fictional university, called Faculty Member.

Faculty Member has a custom field called Department.

Now, I could manually enter this field–Biology, Astronomy, Basket-Weaving, etc., but a better way would be if I could get a drop-down list in my custom field. The drop-down list would give me the departments (another custom post type I previously created). That would be cool, wouldn’t it? It would save typing and also force the content-creator to be consistent with the pre-existing setup.

To do this, we’ll need these elements:

  • A Department custom post type
  • A Faculty Member custom post type (new)
  • An ACF custom field of type Post Object

Let’s assume you already did the first one.

So we’ll make a new custom post type, called Faculty Member. It will be almost the same as the Department, with the names changed.

/** Faculty Post Type */

function create_faculty_posttype()
	$labels = array(
		'name'                => _x('Faculty Member', 'Post Type General Name', 'drupalpress'),
		'singular_name'       => _x('Faculty Member', 'Post Type Singular Name', 'drupalpress'),
		'menu_name'           => __('Faculty Member', 'drupalpress'),
		'parent_item_colon'   => __('Parent faculty', 'drupalpress'),
		'all_items'           => __('All faculty members', 'drupalpress'),
		'view_item'           => __('View faculty', 'drupalpress'),
		'add_new_item'        => __('Add New faculty', 'drupalpress'),
		'add_new'             => __('Add New', 'drupalpress'),
		'edit_item'           => __('Edit faculty', 'drupalpress'),
		'update_item'         => __('Update faculty', 'drupalpress'),
		'search_items'        => __('Search faculty', 'drupalpress'),
		'not_found'           => __('Not Found', 'drupalpress'),
		'not_found_in_trash'  => __('Not found in Trash', 'drupalpress'),
	//other options
	$args = array(
		'label'               => __('faculty', 'drupalpress'),
		'description'         => __('University faculty', 'drupalpress'),
		'labels'              => $labels,
		// Features this CPT supports in Post Editor
		'supports'            => array('title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields',),
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'menu_icon'           => 'dashicons-admin-users',
		'can_export'          => true,

		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'post',
		'show_in_rest' => true,


	register_post_type('faculty', $args);

add_action('init', 'create_faculty_posttype', 0);

Add custom field

We’ll need ACF for the next step. Create a field group for the new custom post type. I’ll call it Department, with the Field Name of faculty_department.

The Field Type will be Post Object.

Now make sure that you put Department into the Filter by Post Type input.

The Post Object Field Type will create a drop-down of Custom Posts.

Filtering it by Department will make only Department custom post type show up. Otherwise, you might let the content creator put an Event as a Department or something like that.

Now that’s basically it. When you make Faculty Member posts, you’ll have access to a drop-down field that lets you choose which Department the Faculty Member belongs to.

Outputting the custom field

Getting this on the page was not as simple as for other custom field. Let’s just take a look at the code.

$custom_field = get_field('faculty_department');
echo $custom_field->post_title;

This is a little obscure.

What happens if you just output $post->faculty_department? Well, the answer in my case is 323. What does that have to do with the Department? Beats me!

You need to get the post_title of the custom field. It could also be done on a single line:

echo $get_field('faculty_department')->post_title;

I think that’s even harder to understand.