Skip Navigation

[Resolved] Split: Call the post title from one CPT into the CRED form of another

This support ticket is created 3 years, 8 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 – 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)

Author
Posts
#1730901

Ok, I think I'm either overcomplicating this in my head or misunderstanding my own instructions 🙂

How do I call the post title from a different CPT into the CRED form of another for this function to work? I don't see the post-title option in the CRED editor. Do I need to figure out the relationship using views and insert it that way? ie [wpv-post-title item="@vehicle-tmna-location.child"] ???

#1730927

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

To better understand this requirement, can you please share a link to an example form, along with the field in which you'd like this post title from another CPT to be automatically called?

I'll be in a better position to suggest the next steps accordingly.

Note: The admin access details from the last ticket no longer seem to work. I'm setting your next reply as private, so that you can share them again, here.

regards,
Waqar

#1732247

also, I finally got around to updateing the function from the previous ticket as you suggested and it brought my site down -

add_shortcode('get_dynamic_select_options', 'get_dynamic_select_options_fn');
function get_dynamic_select_options_fn($atts) {
    $field = $atts['field'];
    $default_text = $atts['default_text'];
    $default_value = $atts['default_value'];
    $data = do_shortcode('[wpv-control-postmeta type="select" field="'.$field.'" url_param="wpv-'.$field.'"]');
    $data_arr = explode('value="', $data);
  
    if(sizeof($data_arr) >= 2)
    {
        for ($i=1; $i < sizeof($data_arr) ; $i++) { 
            $temp_arr = explode('">', $data_arr[$i]);
            if(!empty($temp_arr[1])) {
                $final_data[] = $temp_arr[0];
            }
        }
        if(!empty($final_data))
        {
            ob_start();
            echo '<select class="form-control wpt-form-select form-select select" output="bootstrap" preset_value="" urlparam="" preview="" previewsize="" select_label="" edit_label="" value_escape="" make_readonly="" placeholder="" select_text="" data-wpt-type="select" name="'.$field.'">';
            echo '<option value="'.$default_value.'" selected="selected">'.$default_text.'</option>';
  
            foreach ($final_data as $final_data_item) {
                echo '<option value="'.$final_data_item.'" class="wpt-form-option form-option option" data-wpt-type="option" data-wpt-name="'.$field.'">'.$final_data_item.'</option>';
            }
            echo '</select>';
            return ob_get_clean();
        }
    }
}
#1734211

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for sharing the access details.

In order to dynamically populate the options of the "Vehicle Location" field from the posts from the CPT "TMNA Locations", you'll need to use the filter "wpt_field_options":
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

Example:


add_filter( 'wpt_field_options', 'func_to_dynamically_populate_vehicle_locations', 10, 3);
function func_to_dynamically_populate_vehicle_locations( $options, $title, $type ){
    switch( $title ){
        case 'Vehicle Location':
            $options = array();
 
            // add first empty value
            $options[] = array('#value' => '', '#title' => '---');
 
            // get all tmna-location post items
            $args = array( 'post_type' => 'tmna-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;
}

As a result, when you'll check the "Vehicle Location" field on the "Vehicle" post edit screen or in the form for the "Vehicle" post type, you'll see "TMNA Locations" posts in its select/dropdown.

On form's submission, the selected "TMNA Locations" post will be automatically saved in the "Vehicle Location" field, but if you'd also like to create a relationship between this new "Vehicle" post and the selected "TMNA Locations" post, you'll need to use "toolset_connect_posts" function ( ref:https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts ) through "cred_save_data" hook ( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data ).

Example:


add_action('cred_save_data','func_connect_child_posts',15,2);
function func_connect_child_posts($post_id,$form_data) {
	if ($form_data['id']==1234) {
		if(!empty($_POST['wpcf-vehicle-location'])) {
			toolset_connect_posts( 'tmna-location-vehicle', $_POST['wpcf-vehicle-location'], $post_id );
		}
	}
}

Note: You'll replace 1234 with the actual form's ID.

As for the issue with the code snippet from my previous message, I couldn't reproduce this on my test website.

I'll recommend to turn on WordPress debugging and see if any errors or warnings are shown on the screen or in the server's error logs.
( ref: https://wordpress.org/support/article/debugging-in-wordpress/ )

regards,
Waqar

#1737311

Thanks Waqar! this worked great. Do you have any idea how I can insure that this will work on import? When I import CSV using allimport (w/ the toolset addon) it does not populate this field even though it is selected and the values match.

Larry

#1738299

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Larry,

Thanks for the update and glad that it worked.

Can you please share some information about the exact steps that you took for the import and also share example of the CSV file that was used?

I'll be in a better position to narrow down to the possible missing link/step.

regards,
Waqar

#1739865

Can you please provide a private reply box? Thanks

#1741459

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Larry,

I've set your next reply as private.

regards,
Waqar

#1741691

Thanks Waqar,

In the admin dashboard I goto plugins and reactivate WP All Import & WP All Import - Toolset Types Add-On BETA.

I'm actually going to have to close this for now. The plugin that I'm using is completely broken. And I dont' appear to be the only one with the same issue. Thanks anyway.

Larry

#1741693

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.