Skip Navigation

[Resolved] set the default value of generic cred select field from shortcode.

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

This topic contains 1 reply, has 1 voice.

Last updated by erinJ 5 years, 2 months ago.

Author
Posts
#1489523

I am trying to:

I have a generic select field with its options populated from a shortcode.

This shortcode queries a custom post type "countries". Each value comes from a toolset meta field, country code, and the label is the post title.

Here is what the field looks like on the cred form:

<label>Country</label>
	[cred_generic_field type='select' field='wpcf-country_code']
              {
              "required":0,
          	  "default": [[wpv-user field='wpcf-user_country']],
              "options": [[list_country_options]] , 
              }
            [/cred_generic_field]

and here is the code that generates the json options for the generic field:

function cncrg_get_country_codes(){
    $args = array(
        'post_type'   => 'who-country',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'order_by' => 'title'
    );
    
    
    $country_list = []; 
     
    $countries = new WP_Query($args); 
    
    if ($countries){

        while($countries->have_posts()) : $countries->the_post();
            global $post; 
            $code = get_post_meta( $post->ID , 'wpcf-country_code', true );
            $option = array(
                "value" => $code,
                "label" => $post->post_title
                    );
            $option_j =  json_encode($option);
            array_push($country_list, $option_j); 
        
        endwhile; 
        
        wp_reset_postdata();
    }
    
    $list_options = implode(', ', $country_list);
    return $list_options; 
}

add_shortcode('list_country_options', 'cncrg_get_country_codes'); 

I'm having 2 problems:
1. The options aren't sorting by post title as I've defined in the original query. Instead, they're sorting by the field value.
2. The default value isn't loading from the shortcode. Infact, when I add the default option, the field dissappears all together on the form.

What can I do to resolve these issues?

#1489575

I've updated the code in my functions to sort the array by the 'label' as a workaround for problem 1 listed above.

function cncrg_get_country_codes(){
    $args = array(
        'post_type'   => 'who-country',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'order' => 'ASC',
        'order_by' => 'post_title'
    );   
    $country_list = []; 
     
    $countries = new WP_Query($args); 
    
    if ($countries){

        while($countries->have_posts()) : $countries->the_post();
            global $post; 
            $code = get_post_meta( $post->ID , 'wpcf-country_code', true );
            $option = array(
                "value" => $code,
                "label" => $post->post_title
                    );
            
            array_push($country_list, $option); 
        
        endwhile; 
        wp_reset_postdata();
        
        //declare 'label' key as a column to sort by 
        $label  = array_column($country_list, 'label');

        // Sort the country_list array with label ascending
        array_multisort($label, SORT_ASC, $country_list);
        //error_log('country_list ' . print_r($country_list, true));
        
    }
    
    //encode our array in json format for our generic cred field
    $list_options = json_encode($country_list); 
    //error_log('list_options ' . $list_options);
    return $list_options; 
}

add_shortcode('list_country_options', 'cncrg_get_country_codes'); 

I was able to resolve problem #2 by adjusting my code as follows:

<label>Country</label>
			[cred_generic_field type='select' field='wpcf-country_code']
              {
              "required":0,
          	  "default": ["[wpv-user field='wpcf-user_country']"],
              "options": [list_country_options]  
              }
            [/cred_generic_field]