Skip Navigation

[Resolved] Dynamic selection menu

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/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Waqar 1 year, 9 months ago.

Assisted by: Waqar.

Author
Posts
#2550075

Hi,
I have selection fields with different names (example field1, field2, field3) but they all have the same entries (there are about 150 entries).

Is it possible to manage these fields dynamically from php code? By doing so I could create a function that "fills" the menus of my fields.

Regards

#2550797

Hi,

Thank you for contacting us and I'd be happy to assist.

You can use the 'wpt_field_options' filter to programmatically populate the options for the 'select' type fields:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

If you plan to use some common entries/items for these options, you can register a custom post type, e.g. 'Select Options', and add those 150 options as individual posts in that new custom post type.

Next, you can use a custom function attached to the 'wpt_field_options' filter that gets all the posts from this new post type and generates them as options (post tile being the option title and post ID being the option value) for the target select type field(s).

You'll find an example of a similar function in this forum thread:
https://toolset.com/forums/topic/cpt-call-field-in-a-second-cpt/#post-2207723

I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2553377

Hi Waqar,
thanks for info.

This solution also works with WPML tranlations?
If I create the first field with all the translated entries then "clone" the second one from code will the translations work?

Regards

#2553555

Thanks for writing back.

With WPML, the code needed to generate the options dynamically will become slightly more complicated. That is because the posts in the different languages in that post type for options will have different post IDs.

You'll have to make sure that those selected custom fields always use the default/primary language's post ID as an option value, but the title that is generated for those options uses the current language's post title.

For example, suppose you have 3 select-type custom fields with titles 'Select Field 1', 'Select Field 2', & 'Select Field 3' and in those fields, you'd like to generate the options from the post type with slug 'book'. The code, in this case, will look like this:


add_filter( 'wpt_field_options', 'custom_populate_select_field_options', 10, 3);
function custom_populate_select_field_options( $options, $title, $type ){
	switch( $title ){
		case 'Select Field 1':
		case 'Select Field 2':
		case 'Select Field 3':

			// get primary language code
			$default_lang = apply_filters('wpml_default_language', NULL );

			// get current language code
			$current_lang = apply_filters( 'wpml_current_language', NULL );

			// get the list of posts from the target post type
			$main_args = array(
				'post_type'        => 'book',
				'posts_per_page'   => -1,
				'post_status'      => 'publish',
			);

			$posts_array = get_posts( $main_args );
			foreach ($posts_array as $post) {
				$args = array('element_id' => $post->ID, 'element_type' => 'post' );
				$post_language_code = apply_filters( 'wpml_element_language_code', null, $args );
				// make sure only the posts in the primary language are included as options
				if($default_lang == $post_language_code) {
					$options[] = array(
						'#value' => $post->ID,
						'#title' => get_the_title(apply_filters( 'wpml_object_id', $post->ID, 'post', FALSE, $current_lang )),
					);
				}
			}
		break;
	}
	return $options;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Notes:

1. You'll need to adjust the target select type field slugs and the target custom post type's slug in this code snippet, as per your website.

2. In these select-type custom fields, please make sure that the option 'Copy from original to translation' is selected, so that they have the same value in the translated posts.