Skip Navigation

[Resolved] Add a select custom user field with options automatically generated from a CPT

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to automatically generate the options in a user custom field select dropdown from a custom post type

Solution:
You can use the wpt_field_options filter to dynamically fill a select field's options.

It is not documented. Below is some sample code which populates a field called "Choices" with the posts of the custom type "thing".

/**
 * Auto-populate custom User Field
 */
add_filter( 'wpt_field_options', 'tssupp_populate_user_field', 10, 2 );
 
function tssupp_populate_user_field( $current_options, $title_of_field ){
 
    if ( 'Choices' == $title_of_field ) {
 
        $current_options = array();
 
        $args = array(
            'post_type'     =>   'thing',
            'numberposts'   =>   -1
        );
        $posts = get_posts( $args );
 
        foreach ($posts as $key => $post) {
 
            $current_options[] = array(
                '#title'    =>   $post->post_title,
                '#value'    =>   $post->ID
            );
        }
    }
 
    return $current_options;
}

100% of people find this useful.

This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by rafaelL-2 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#622895
example.png

Tell us what you are trying to do?

I have a custom post called "labs".

Every "lab" has only two text fields. Name and Email

Lets imagine I have a lab called Lab01 with email "example@example.com"

So I want to add a select box with that info to be able to select from wp-users like this (see example)

At the end, from normal wp-users (wp-admin/users.php) Will be able to select what lab we want to select

Is there any documentation that you are following?
I think I found similar ticket but is not what I am looking for :https://toolset.com/forums/topic/i-want-to-create-a-nationality-list-as-a-user-field-via-the-types-plugin/

Regards!

#622929

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

I'm not quite sure I follow.

You want a select custom user field, and you want the options for that select dropdown to be automatically populated by posts of a custom type, is that right?

#622937

Totally right. Let me give a one more example. Just to be sure

My custom post, called "LAB" has currently two post:

Name: LAB01, email: example01@example.com
Name: LAB02, email: example02@example.com

So, from wp-admin/users.php, the select field will be populated from the current post type LAB, more specifically from the name field as you see on the image

Regards

#622947

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Rafael

You can use the wpt_field_options filter to dynamically fill a select field's options.

It is not documented (on our to-do list), so I put together some sample code which populates a field called "Choices" with the posts of the custom type "thing", which you should be able to adapt to your needs:

/**
 * Auto-populate custom User Field
 */
add_filter( 'wpt_field_options', 'tssupp_populate_user_field', 10, 2 );

function tssupp_populate_user_field( $current_options, $title_of_field ){

	if ( 'Choices' == $title_of_field ) {

		$current_options = array();

		$args = array(
			'post_type'		=>	'thing',
			'numberposts'	=>	-1
		);
		$posts = get_posts( $args );

		foreach ($posts as $key => $post) {

			$current_options[] = array(
				'#title'	=>	$post->post_title,
				'#value'	=>	$post->ID
			);
		}
	}

	return $current_options;
}
#623014
show01.png

Hi again, thanks for your solution. However seems doesn't work

I did the following:

1-I create a select field called "labasociadoemail"

2- then I change on your code 'choices' by 'labasociadoemail' and 'thing' to my custom post type 'laboratorio' (as you can see at the bottom of the image.

You can see the result on the image. I understand your code. However I don't get why you do this IF:

  if ( 'Choices' == $title_of_field ) { 

I tried to remove the IF but still doesn't work.

Regards

#623028

Seems I deactivate the snippet by a failure, so I am happy to say It's working