"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.