Skip Navigation

[Resolved] Populate select field with list of posts in a custom post type

This support ticket is created 7 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.

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 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 17 replies, has 2 voices.

Last updated by hugoC-3 7 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#563647

I have the same issue discussed in
https://toolset.com/forums/topic/custom-field-type-that-list-posts-in-a-custom-post-type-or-custom-taxonomy/.
I would like to populate a select field with the list of all posts on my site.

I created a custom fields group, added a select field and added it to a new cpt.
I added the code you shared to my theme's functions.php and changed 'my-select-field' with the slug of my select field.

Unfortunately nothing happens: when I create a new custom post the select field still shows 'option 1' instead of a list of posts.

Did I overlook something?

#563651

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

Thank you for contacting our support forum.

We actually have some custom code to get this to work for you.

Add the following to your functions.php file



add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Team leader':
            $options = array();
			$args = array(
                'post_type'        => 'team-leader',
                'post_status'      => 'publish',
				'meta_query' => array(
        							array(
							            'key'     => 'wpcf-active',
							            'value'   => 'Yes',
							            'compare' => '=',
        								),
									),
								);
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $first_name = get_post_meta($post->ID,'wpcf-first_name');
                $last_name = get_post_meta($post->ID,'wpcf-tmsurname');
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $first_name[0]." ".$last_name[0],
                     );
                }
               
                break;
}
    return $options;
}

To Get this to work you will need to replace the Text “Team Leader” with the name of the select field you wish to populate. then within the foreach you can define the fields that you want to show in the select field.

Please let me know if this helps or if further assistance is needed to get it working fully.

Thanks,
Shane

#564774

Thanks for this solution.
Since I am not a programmer I don't completely understand it though.
In my case I need a list of standard WP posts, so I changed 'team leader' to 'post' but no success so far.
I would like to show the post title in the foreach loop.

#564779

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

Let me refine the code for you so that it should just work when added to the site.

add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Team leader':
            $options = array();
            $args = array(
                'post_type'        => 'post',
                '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;
}

Change 'Team Leader' to the name that you gave the select custom field and let me know if this works.

Thanks,
Shane

#565241

Unfortunately I get a blank page when I change the name of my custom field and add this to my functions.php ????

#565350

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

Would you mind providing me with FTP and Admin access to the website so that I can set this up for you?

Also could you send me a link to the a page where I can see the select field?

Thanks,
Shane

#565671

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

I've resolved the issues with the code and it should now be working.

Thanks,
Shane

#565867

It works fine now, thank you!
Two more questions:
1. Is it possible to add a line like "Select an option" above the options list?
2. is multiselect possible? Can a user CTRL-select multiple options?

#565893

And one more question: how can I display the selected option value on the site? At the moment it just shows the id, while I need the title with a link.

#565936

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

That is correct because we are storing the post id as the select value. To get the title you can simply do this.

[wpv-post-title id='types shorcode  goes here']

Just place the shortcode that displays dropdown fields in the ID parameter and it should give you the title.

Please let me know if this helps.

Thanks,
Shane

#565966

I tried this:
[wpv-post-title id='[types field='waar-ben-ik-mee-bezig'][/types]']
but it doesn't work: it doesn't return the option selected, but the title of the custom post.

#565997

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

Could you let me know which page you are testing this on so that I can have a look.

Thanks,
Shane

#566056

I am testing on the cpt "Veranderaar' like this one: hidden link

#566461

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Hugo,

I've updated the code so that it stores the post title instead of the post id for the select value.

What you need to do is to go through and re-save the posts and it should now work.

Thanks,
Shane

#566515

Thanks a lot, it works now!
One last question: instead of just the title I would like this to be a link to the post.
How can that be done?