Skip Navigation

[Resolved] Results that fill the select box are numeric values and not the info I need.

This support ticket is created 5 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1188315
Capture.PNG

I am trying to: Populate a select box in a custom post field with values from a different custom post type.

Link to a page where the issue can be seen: This is not happening on the front end of the site, but rather on the back end.

I expected to see: the title of the custom post type to populate the select box.

Instead, I got: Numeric pairs of values. ie: 1-1; 1-2; 1-3; 1-4, etc. I noticed that if the custom post type that is supposed to be populating the select box has 4 post entries, than the select box is filled with 5 numeric value pairs. If the CPT had 6 post entries, than the select box would be populated with 7 numeric value pairs. The result is always 1 more value than what is actually present in the CPT.

#1188794

Hello,

Please provide detail steps to duplicate the same problem:
Are you using custom PHP codes to populate the options in field "Drive list"?
If it is, how do you setup the custom codes?
Is there any related post type relationships? How do you setup the post type relationships?

#1189261

I did not set up any post type relationships....is that something that is required in order to make it work? And yes, I used custom PHP code, most of which I took from an old thread on the Toolset website that I came across when someone else was trying to accomplish the same task, but I was unable to get it to work properly, so I customized it a bit. Here is the custom PHP code that I used:

function my_wpt_field_options( $options, $title, $type ) {
    switch( $title ) {
    case 'Driver List':
        $options[] = array();
			$args = array(
				'post_type'   => 'employee',
				'post_status' => 'publish',
			);
			$posts_array = get_posts( $args );
			foreach ($posts_array as $post) {
				$first_name = get_post_meta($post->ID, 'employee-first-name');
				$options[] = array(
					'value' => $post->post_ID,
					'title' => $first_name,
				);
			}
        	break;
    }
    return $options;
}
add_filter('wpt_field_options', 'my_wpt_field_options', 10, 3);

Here is the setup I'm trying to make work:
I have a custom post type called Employees, which simply lists different employees of a company. Then, I created a custom post type called DINs, which is where each employee can be assigned a unique identifier number whenever they become a driver for the company. So I'm trying to have a select box called Driver List, inside of the Add a new DIN form, that populates with all the employees that I have already added inside of the Employees CPT.

Please let me know what I need to do in order to make this work.

Thanks so much for your help.

#1189503

I assume you are going to display field "employee-first-name" value as option's title.

How do you setup the custom field "employee-first-name", if it is created with Types plugin, Types plugin will pre-pend slug "wpcf-" before the field slug, so in your case, you need to change this line from:

$first_name = get_post_meta($post->ID, 'employee-first-name');

To:

$first_name = get_post_meta($post->ID, 'wpcf-employee-first-name');

For the problem:
The result is always 1 more value than what is actually present in the CPT.

I suggest you try to replace this line from:

$options[] = array();

To:

$options = array();

And test again

#1189931
Capture.JPG

I followed your suggestions, but I'm still running into an issue: The number of results that were added into the select box is no longer adding 1, so it is returning the proper number of results. However, the results being returned are still showing up with a display of 1-1, 1-2, 1-3, 1-4, 1-5 instead of displaying the name of the employee from the Employees Custom Post Type. Why is this happening?

I attached another screenshot for you to see the values that are being returned. However, the select box is supposed to be populated with the first name of each employee that has been added into the Employee CPT. It's returning the correct number of entries (5), but the values being returned are confusing to me.

Thanks so much.

#1190042

Please check these:
Deactivate other plugins and switch to wordpress default theme, and test again

If the problem still persists, since it is a custom PHP codes problem, please provide a test site with the same problem, also point out the problem page URL and where I can edit your PHP codes, I need a live website to test and debug it. thanks