Skip Navigation

[Resolved] auto populate a select field

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 7 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#571351

I am trying to do something simple ( i hope)

The background,

I have two cpt's. Supplements and Case study.

In "case study" I have a Select field that I want to be populated with the content from the Supplements CPT.

The supplements CPT named "Suppliments" contain only 1 field single line "name"

The Casestudy cpt named "Case study" slug "case-study" has a select field "Supplements" slug "supplements"

I need all the supplements entries from the cpt named "Suppliments" to pupulate the select field (this field allows for multiple entries) in the Case study cpt.

I have tried some code but i am still not successful

A ssummery of what i am truying to do:

The source data for the select field will come from "suppliment" and the select field is in the "Case study" with the field called "name"

The code I have was use on another site , hiwever my php is not that hot. I tried to alter it but to no avail

<?php
/**
 * Generate child theme functions and definitions
 *
 * @package Generate
 */
  
add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
    switch( $title ){
        case 'Suppliments':
            $options = array();
            $args = array(
                'post_type'        => 'suppliment',
                'post_status'      => 'publish',
                'meta_query' => array(
                                    array(
                                       
                                        ),
                                    ),
                                );
            $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;
}

#571495

I need all the supplements entries from the cpt named "Suppliments" to pupulate the select field (this field allows for multiple entries) in the Case study cpt.
Types doesn't offer a multiple select custom field, so if you need to select multiple options you must use checkboxes. However, that presents a different problem, because wpt_field_options doesn't filter checkbox options, only select (single) and radio fields. Right now, there's not a good way to solve this problem with a custom field. Instead, you could create a many-to-many relationship between Supplements and Case Studies. This would allow you to select one or more Supplements to associate with a Case Study in wp-admin. Otherwise, you're looking at manually maintaining the list of Supplements as a checkboxes custom field.

More information about many-to-many relationships:
https://toolset.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/