I want to add a select field to a custom post type that allows the user to select from any of the countries in the world. How can I add this without typing them in one by one?
Thank you!
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
Hello. Thank you for contacting the Toolset support.
Well - do you have a database table for countries that holds all countries entry?
No, I just have them in a list.
Hi,
Minesh is on vacation, your will get the answer in tomorrow morning
Minesh
Supporter
Languages:
English (English )
Timezone:
Asia/Kolkata (GMT+05:30)
When you say, list - what kind of list you have?
There is no such feature available but you can add user custom field as select and later you can use Types hook "wpt_field_options" to fill out the dropdown option for your county dropdown box dynamically.
I suggest creating a country table within your database using the following country list sql:
=> https://github.com/raramuridesign/mysql-country-list/blob/master/mysql-country-list.sql
And then create a custom select field "country-list" and add the following field to your:
For example - add the following code to your current theme's function.php file OR you can add it to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
function func_dynamic_populate( $options, $title, $type ){
global $wpdb;
switch( $title ){
case 'country-list':
$options = array();
// replace your table name iwth myTable
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
$posts_array = $result
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->country_name,
);
}
break;
}
return $options;
}
Where:
=> You should change 'contry-list' to your custom field name if required.
=> Feel free to adjust the above as as required, if needed.