Skip Navigation

[Resolved] Custom field with multiple values to be displayed in a specific order

This support ticket is created 3 years, 1 month 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)

This topic contains 6 replies, has 2 voices.

Last updated by gregoireL-2 3 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1964439

Tell us what you are trying to do?

I have a custom post type called Project. It has 2 custom fields, one for the Countries where the Project is taking place on, and Institutions for the institutions that take part in that project.

When displaying the project in the view, I want to display the list of countries, with the first one being the main country. Same thing for institutions, I need to display a list, with the first one being the leader.

I am wondering if there is a custom field type that allows the selection of multiple values from a predefined list (Like the checkbox field type), but that allows ordering of the values?

Another solution I am investigating is splitting the field Countries into 2 fields: "Main country" which will have only 1 value, and "Other countries" which will be a multi-value. This would fix my issue in terms of displaying the list in the view, but it would make it impossible to then filter for a specific country, and seeing projects that have that country either as the main country or other country.

Is there any documentation that you are following?
https://toolset.com/course/custom-types-in-wordpress/

Is there a similar example that we can see?

What is the link to your site?
hidden link

#1964977

Hello,

There isn't such kind of built-in feature within Toolset plugins, Toolset Types plugin provide custom checkboxes field:
https://toolset.com/course-lesson/creating-custom-fields/
But there isn't the "Main option" feature.

For your "Another solution":
You can setup all values in "Other countries" field, so you can also filter the result by "Other countries" field, for example:
Post A:
- "Main country": USA
- "Other countries": USA, UK ...

And when you display the Project post, use custom codes to hide the "Main country" value from "Other countries", for example create another custom shortcode with a PHP function:
https://developer.wordpress.org/reference/functions/add_shortcode/
In this PHP function, get the "Main country" and "Other countries" field values,
https://developer.wordpress.org/reference/functions/get_post_meta/
remove "Main country" value from "Other countries" field output

#1967223

Hi Luo,

Thanks for the detailed response. It makes sense to use the former solution then. I am just a bit lost on what code I should add into the shortcode to display what I need. I am currently using the "[types field='countries' separator=' | '][/types]" shortcode to display the list of countries.
Can you provide the code that this shortcode uses so I can add my custom logic to it?

#1967539

I assume the "Main country" field is a custom select field created with Toolset Types plugin, the field slut is "main-country"

If it is, please try these:
1) Add below codes in your theme file "functions.php":

add_shortcode('remove_main_country', function($atts, $content){
	$atts = shortcode_atts( array(
        'countries_slug' => 'countries',
        'main_country_slug' => 'main_country',
		'separator' => ' | ',
    ), $atts );
	$separator = $atts['separator'];
	$countries = do_shortcode("[types field='" . $atts['countries_slug'] . "' separator='" . $separator . "'][/types]");
	$main_country = do_shortcode("[types field='" . $atts['main_country_slug'] . "'][/types]");
	if($main_country){
		$arr = explode($separator, $countries);
		foreach($arr as $k=> $v){
			if(strtolower($v) == strtolower($main_country)){
				unset($arr[$k]);
				break;
			}
		}
		$countries = implode($separator, $arr);
	}
	return $countries;
});

2) Use above shortcode like this:
[remove_main_country separator="|" main_country_slug="main-country"]

#1982921

Hi Luo, that works great for removing the main country from the list!
It would help me if the shortcode finished by adding the main country at the beginning of the array, that way the list is already displayed properly without me having to display the main country separately.

Thanks!

#1983263

Please change the PHP codes as below:

add_shortcode('remove_main_country', function($atts, $content){
	$atts = shortcode_atts( array(
        'countries_slug' => 'countries',
        'main_country_slug' => 'main_country',
		'separator' => ' | ',
    ), $atts );
	$separator = $atts['separator'];
	$countries = do_shortcode("[types field='" . $atts['countries_slug'] . "' separator='" . $separator . "'][/types]");
	$main_country = do_shortcode("[types field='" . $atts['main_country_slug'] . "'][/types]");
	if($main_country){
		$arr = explode($separator, $countries);
		foreach($arr as $k=> $v){
			if(strtolower($v) == strtolower($main_country)){
				unset($arr[$k]);
				break;
			}
		}
		$arr = array_unshift($arr, $main_country); // move the main country to the top
		$countries = implode($separator, $arr);
	}
	return $countries;
});

And test again

#1984049

Hi Luo, I tested the code you gave me and it did not work off the bat. I debugged it and fixed the issue, and now it works!

You need to replace line 18 by:
array_unshift($arr, $main_country); // move the main country to the top

Since the function returns an int, not the new array.

Thanks for your help!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.