How to use user metadata in post types via Advanced Custom Fields in WordPress

Note: This article is unfortunately outdated. Please se this tutorial on how to create your own acvanced custom field types.

To get the most out of this article a basic knowledge of PHP, WordPress, custom post types and Advanced Custom Fields is required.

If you’ve ever needed custom data input for your WordPress posts or pages (or any other kind of custom post type) then Advanced Custom Fields (ACF) is the shit! It’s flexible, extensible and frequently improved by Elliot Condon. Simply brilliant!

I’ve been using ACF in a couple of projects now and in the current one I needed to be able to present WordPress users as hosts for different kinds of events, showing user a profile photo and a custom user metadata type. There was no obvious, flexible and easy to administer option available, or at least I couldn’t find one, so I started thinking about what could be done with ACF and user profile custom fields. I decided to try it out.

Adding metadata to the user profile

To start with I had to add a couple of custom metadata fields to the user profile.


		



Creating the User Meta field type

To be able to nicely select users as hosts in the custom post type Event I had to create a new Advanced Custom Field type; User Meta. It is in many ways a rip-off of the Select core field type differenting in the way that it is not configurable with respect to options (the values of the select input field) but the options are populated with all the registered WordPress users, instead.

Here’s the code. I’ve kept most of the code in the create_field function to support ACF field options, that might be a requirement for some:

name = 'user_meta'; // variable name (no spaces / special characters / etc)
		$this->title = __("User Meta",'acf'); // field label (Displayed in edit screens)

   	}

	function create_field($field)
	{
		global $wpdb;

		// get the wordpress users
		$wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY display_name");

		$choices = array();

		// loop through users and add them as choices
		foreach ( $wp_user_search as $user ) {
			$user_id = (int) $user->ID;
			$display_name  = stripslashes($user->display_name);
			$choices[$user_id] = $display_name;
		}

		$field['choices'] = $choices;

		// defaults
		$field['value'] = isset($field['value']) ? $field['value'] : array();
		$field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false;
		$field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : false;
		$field['optgroup'] = isset($field['optgroup']) ? $field['optgroup'] : false;

		// no choices
		if(empty($field['choices']))
		{
			echo '

' . __("No choices to choose from",'acf') . '

'; return false; } // multiple select $multiple = ''; if($field['multiple'] == '1') { $multiple = ' multiple="multiple" size="5" '; $field['name'] .= '[]'; } // html echo ''; } } ?>

So now, I can use this field as any other basic Advanced Custom Field; as standalone, in repeaters and flexible content. It’s a very simple solution that stores the user id as value which can then be retrieved in templates to use when fetching information about the user via get_userdata($user_id);

Defining a User Meta field in the event ACF group.
Using the User Meta field to assign hosts to the event post type.

Implementing it in templates

Below I will list a piece of code where the field is used in a repeater, listing event hosts.

Hosted by

ID)): $user_id = get_sub_field('event_host', $post->ID); $user_info = get_userdata($user_id);?>

display_name; ?>

jw_title; ?>

The result

The final result.

To learn more about how to create your own Advanced Custom Field types, see this tutorial.

This Post Has 3 Comments

  1. I was super happy to find this tutorial. However I can’t completely get it to work.
    Line 57 in the user_meta.php fails with syntax error: unexpected ‘=’, expecting ‘,’ or ‘;’
    the problematic line is:
    echo ”;

    if I remove the last bits – ‘=”” .=”” $multiple=””>’; – the syntax error no longer appears, but the acf fails to load field options when I choose the field type.

    I am new to acf, so it is entirely possible that I have missed something somewhere.

    Please help?

  2. Giant blocks of uncommented code and no instructions on which file to add the code to. This was a promising tutorial because I want to accomplish what the author did, but the explanation is too poor to be useful. Disappointing.

  3. Hi Maija and Nick,

    I’m sorry but this article is unfortunately outdated and does not work with the version of Advanced Custom Field Types of today. There is, though, a new Field Type template project on GitHub that you might take a look at that could help you in converting to the new standards.

    https://github.com/elliotcondon/acf-field-type-template

    Sorry for the inconvenience!

Leave a Reply

Close Menu