[Resolved] Use custom select field with values from custom post titles
This support ticket is created 4 years 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?
- An editor user should be able to choose a project title in a select field
The project is a custom post type (Projecten) and the select field need to be available in the custom post type Kinderen
Is there any documentation that you are following?
- Not that I found
Is there a similar example that we can see?
- Not that I know
If you just need to store the title then you have two options:
1. you can add a select custom field to your Kinderen post type to store the Projecten post. Add a dummy option when you set up the custom field. Then use the API filter wpt_field_options to dynamically populate the field options with the post titles coming from your Projecten posts.
2. if you are comfortable using the legacy editor for Views you can add a generic select field to your form, and then use a View to generate the options.
We can give you more guidance on one of these, if you let us know which of the above best suits your needs.
Thank you for your reply! I think option 1. would be sufficient, but since I'm not too familiar with your terminology, I need some help with the steps needed 🙂
As mentioned by Nigel you need to create a custom field of select type and associate it with the Kinderen post type
1) Lets name the select field as Projecten Title (projecten-title)
2) Do not add any Select Options, save the fields group
3) Adding Projecten post type titles as options to the Select fields
- Since you want to display all the Projecten Title on the select field you can make use of the filter wpt_field_options to generate values dynamical
add_filter( 'wpt_field_options', 'generate_projecten_title', 10, 3);
function generate_projecten_title( $options, $title, $type ){
switch( $title ){
// run this code only if the field name is projecten-title
case 'projecten-title':
$options = array();
$args = array(
'post_type' => 'projecten', // replace with appropriate post type anme
'post_status' => 'publish');
// Queries all the published posts of post type projecten
$posts_array = get_posts( $args );
foreach ($posts_array as $post) {
$options[] = array(
'#value' => $post->post_title,
'#title' => $post->post_title,
);
}
break;
}
return $options;
}
The piece of code can be added to the theme functions.php file
Thanks a lot for your help. I managed to set the values in the select field using your code example. 😊
A couple more questions:
1. I'm only seeing 5 titles in the select field, while there are 16 projects. How to get all project titles in the select field? (All projects are published).
2. How to set an empty default value? (The first value in the list is displayed as default value now)
Glad that helps. However your followup questions are not related to Toolset rather general programming stuff, let me help you with some pointers
1) The number of post is controlled by the "numberposts" argument in get_posts function by default it retrieves only 5 posts, In case if you need to retrieve all posts you shall use -1
$args = array(
'numberposts' => -1,
'post_type' => 'projecten', // replace with appropriate post type anme
'post_status' => 'publish');