[Resolved] Custom select field pulling dynamic data
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.
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.
"I have a post field group from a custom post type (Products) that uses a custom select field ('winery-select'). I would like this custom select field to be dynamic and pull data from my other custom post type (winery)."
The snippet has been created for the case and used to work fine. However, now it's not pulling data anymore.
Here's the snippet's code:
<?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' => 'winery',
'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;
}
Hi Shane, thanks for your quick response. Could I know the issue, so I can check it later if it suddenly happens again?
Also, the same field is giving me trouble, not loading all its variables on the view search I created.
Do you think that could be related to the first problem?
The issue was with the field's name in the code. There was a common 'w' when it should've been captailized.
Secondly given that there isn't a predefined list of items in the custom field database it will only show the items that are selected. This means it won't show all the available options only the ones that have already been selected from your posts.
This is one of the drawbacks when dynamically populating the select field.