Problem:
How to automatically generate the options in a user custom field select dropdown from a custom post type
Solution:
You can use the wpt_field_options filter to dynamically fill a select field's options.
It is not documented. Below is some sample code which populates a field called "Choices" with the posts of the custom type "thing".
/** * Auto-populate custom User Field */ add_filter( 'wpt_field_options', 'tssupp_populate_user_field', 10, 2 ); function tssupp_populate_user_field( $current_options, $title_of_field ){ if ( 'Choices' == $title_of_field ) { $current_options = array(); $args = array( 'post_type' => 'thing', 'numberposts' => -1 ); $posts = get_posts( $args ); foreach ($posts as $key => $post) { $current_options[] = array( '#title' => $post->post_title, '#value' => $post->ID ); } } return $current_options; }
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 |
---|---|---|---|---|---|---|
- | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | 7:00 – 14:00 | - |
- | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | 15:00 – 16:00 | - |
Supporter timezone: Europe/London (GMT+00:00)
This topic contains 5 replies, has 2 voices.
Last updated by 6 years, 9 months ago.
Assisted by: Nigel.