This support ticket is created 3 years, 2 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Tell us what you are trying to do?
I'm creating a post field group from a custom post type (Products) that uses a custom select field. I would like this custom select field to be dynamic and load data from another custom post type (Wineries):
I've already created a new snippet called 'winery_selection' in the Toolset Custom code snippets and added the code above.
However, I'm in doubt about how to twitch it to adjust my situation.
1) Where the function asks for the 'field-name' that I should personalize, should I write the new select field name I'm creating (Winery-producer) or the old one from which I want info to be loaded (Winery)?
2) Where the function is asking for a 'post_type' that I should personalize to adjust my case, is it the custom post type where the data is created (Wineries) or the other custom post where I want data to be loaded to be selected (Products)?
I hope my question is clear.
1) Where the function asks for the 'field-name' that I should personalize, should I write the new select field name I'm creating (Winery-producer) or the old one from which I want info to be loaded (Winery)?
That is correct you will put the slug of your custom field.
2) Where the function is asking for a 'post_type' that I should personalize to adjust my case, is it the custom post type where the data is created (Wineries) or the other custom post where I want data to be loaded to be selected (Products)?
You will put the Post Type slug that you want the function to pull the list from. SO if your select field is on the Products Post type then you will use the Wineries post type slug.
So, I gave the function the "wineries" post_type slug and put the slug of the custom field I'd like data to be pulled from ('winery').
How is the function going to recognize I want it to dynamically populate the options of my select custom field now ('Winery-select')? How do I connect the function with my custom select field?
This is my snippet code:
<?php
/**
* Dynamic content for 'Winery-select' custom select field in custom Post type. */
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
function func_dynamic_populate( $options, $title, $type ){
switch( $title ){
case 'winery':
$options = array();
$args = array(
'post_type' => 'wineries',
'post_status' => 'publish');
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}
Ok, I re-read everything and realized my coding was wrong since the custom field I had to put in it was the selection one (winery-select), not the feeder one. In that case, how will the function know I need it to pull the info from the slug 'winery'?
Needless to say, the prev screenshot is still valid, since it's still not working. 😀
--
This is my code now:
<?php
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
add_filter( 'wpt_field_options', 'func_dynamic_populate', 10, 3);
function func_dynamic_populate( $options, $title, $type ){
switch( $title ){
case 'winery-select':
$options = array();
$args = array(
'post_type' => 'wineries',
'post_status' => 'publish');
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->ID,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}