Problem:
The customer asked how the 'wpt_field_options' filter can be used to dynamically generate a long and alphabetically ordered list of options for a select type field.
Solution:
Shared some details and a code snippet example to show how this filter can be used to generate options from a custom post type:
add_filter( 'wpt_field_options', 'func_to_dynamically_populate_shop_locations', 10, 3);
function func_to_dynamically_populate_shop_locations( $options, $title, $type ){
switch( $title ){
case 'Book Shop Locations':
$options = array();
// add first empty value
$options[] = array('#value' => '', '#title' => '---');
// get all tmna-location post items
$args = array( 'post_type' => 'shop-location', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );
$results = get_posts( $args );
if ( $results ) {
foreach ( $results as $post ) {
$options[] = array('#value' => $post->ID, '#title' => $post->post_title);
}
}
break;
}
return $options;
}
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_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 |
|---|---|---|---|---|---|---|
| - | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | 9:00 – 13:00 | - |
| - | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Karachi (GMT+05:00)
This topic contains 6 replies, has 2 voices.
Last updated by 3 years, 12 months ago.
Assisted by: Waqar.