Skip Navigation

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 6 replies, has 3 voices.

Last updated by jan-karelB 4 years ago.

Assisted by: Raja Mohammed.

Author
Posts
#1835717

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

What is the link to your site?
hidden link

Kind regards,
J Karel

#1836237

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You mean you have a front-end form to publish/edit Kinderen posts, and you would like a select dropdown where the user can choose a Projecten post?

Should the posts be connected in any way, or you just want to be able to save the title of the Projecten post alongside the Kinderen post?

If they need to be connected, you would use post relationships, as described here https://toolset.com/course-lesson/how-to-set-up-post-relationships-in-wordpress/

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.

#1838563

Hi Nigel,

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 🙂

Hope you're able to provide these.

Kind regards,
-J Karel

#1838609

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello Karel,

I will help you with the steps,

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

https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

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

I hope this helps better,

let me know if you need more clarification

Kind regards
Raja

#1838673
Select values.png
Select field.png

Hi Raja,

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)

Kind regards,
- J Karel

#1838717

Raja Mohammed
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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');

Refer : https://developer.wordpress.org/reference/functions/get_posts/

2) Just before the for each loop add the empty value to the options variable

$options[] = array('#value' => '', 
                    '#title' => '--Select posts---',
                );
foreach(...

Regards
Raja

#1838791

Excellent support and all my questions are answered. Thank you!