Skip Navigation

[Résolu] Dynamic Field values from CPT

This support ticket is created Il y a 3 années et 9 mois. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

Auteur
Publications
#1757329

That isn't the correct field. The field that is needed is the vehicle-location field. What you are displaying is the garage name.

#1758063

Waqar
Supporter

Languages: Anglais (English )

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

First, as explained in the other ticket ( ref: https://toolset.com/forums/topic/view-with-weekday-filtering/#post-1758055 ), please make sure that the Divi child theme is active on the website so that your custom functions can work on the website.

A challenge on your website is that there are a lot of custom field groups at WP Admin -> Toolset -> Custom Fields -> Post Fields and they're set to repeat on multiple post types, even when it doesn't seem that those fields are needed for all those post types.

Once your custom functions are working, please share details about the field group in which this "vehicle-location" field exists that you'd like to show in the column "Garage" in the "All Vehicles" table on the home page.

#1758689

Thanks again Waqar,

One additional question, which I think we covered earlier but I wanted to double check, if I add another function to do something similar (pull the titles of the 'cameras' CPT for use in post forms of the 'camera issues' CPT will this work:

// Dynamically populate camera post titles for camera issue creation


add_filter( 'wpt_field_options', 'func_to_dynamically_populate_camera_locations', 10, 3);
function func_to_dynamically_populate_camera_locations( $options, $title, $type ){
    switch( $title ){
        case 'Camera Title':
            $options = array();
  
            // add first empty value
            $options[] = array('#value' => '', '#title' => '---');
  
            // get all camera-title post items
            $args = array( 'post_type' => 'camera', 'posts_per_page' => -1, 'post_status' => 'publish','orderby' => 'title' );
  
            $results = get_posts( $args );
            if ( $results ) {
                foreach ( $results as $post ) {

            return ob_get_clean();
        }
    }
}

  

// Create relationship between Camera & Camera Issue

add_action('cred_save_data','func_connect_child_camera_posts',15,2);
function func_connect_child_camera_posts($post_id,$form_data) {
    if ($form_data['id']==7451) {
        if(!empty($_POST['wpcf-camera-title'])) {
            toolset_connect_posts( 'camera-camera-issue', $_POST['wpcf-camera-title'], $post_id );
        }
    }
}

Thanks!

#1759109

Waqar
Supporter

Languages: Anglais (English )

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

Your understanding is correct and though the example snippet that you've shared doesn't seem to be complete, you've got the basic idea, right.

You'll just copy/paste the working code snippet from my previous replies, make changes into function names, slugs, fields, IDs etc and then it should work for more cases too.

#1761563

I think I'm missing someting. I double checked and when I added this I got the whole critical site error thing...

// Dynamically populate camera post titles for camera issue creation

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


// Create relationship between Camera & Camera Issue

add_action('cred_save_data','func_connect_child_camera_posts',15,2);
function func_connect_child_camera_posts($post_id,$form_data) {
    if ($form_data['id']==7451) {
        if(!empty($_POST['wpcf-camera-title'])) {
            toolset_connect_posts( 'camera-camera-issue', $_POST['wpcf-camera-title'], $post_id );
        }
    }
}

What did I forget?

#1762135

Waqar
Supporter

Languages: Anglais (English )

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

I've compared this code snippet with the one from my original message ( ref: https://toolset.com/forums/topic/split-call-the-post-title-from-one-cpt-into-the-cred-form-of-another/#post-1734211 ) and have also tested this on my website, but couldn't reproduce any issue.

Can you please make sure that there is no repetition of functions of same names, i.e. "func_to_dynamically_populate_camera_locations" or "func_connect_child_camera_posts"?

You can also turn on WordPress debugging ( ref: https://wordpress.org/support/article/debugging-in-wordpress/ ) and see if any errors/warnings are shown on the screen, in an email to the website's admin, or in the server's error logs, when the code is added.

#1763023

I thought it might be easier if I provide the entire function sans the above?


<?php

//Force title for Add Record from VIN or camera field.
add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
 
$forms = array( 942,2101,937,6944,8712,8755,11018,2285,2267,6932,1469,2418,6976,7463,6929,2206,7451,2095 );
if ( in_array($form_data['id'], $forms) ){ 
$vin = get_post_meta($post_id, 'wpcf-vin', true);
$camera = get_post_meta($post_id, 'wpcf-camera-name', true);
 
$post_title=$vin. ' ' .$camera ;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}

// add userid to cred

add_filter( 'wpt_field_options', 'add_some_options', 10, 3);
function add_some_options( $options, $title, $type )
{
    switch( $title )
    {
    case 'user id':
        $args = array(
            'orderby' => 'display_name',
            'fields' => array( 'ID', 'display_name')
        );
        foreach( get_users($args) as $user ) {
            $options[] = array(
                '#value' => $user->ID,
                '#title' => $user->display_name,
            );
        }
        break;
    }
    return $options;
}


// show pending posts in view

function ts_filter_query( $view_args, $view_settings, $view_id )  {
   
  if ( in_array( $view_id, array( 11008 ) ) ) { // list of View IDs to apply this to
     
    $view_args['post_status'] = array( 'publish', 'pending' );
  }
   
  return $view_args;
}
add_filter( 'wpv_filter_query', 'ts_filter_query', 101, 3 );


// Add select field values for TMNA Locations


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();
        }
    }
}


// Dynamically populate TMNA Location data for vehicle creation

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;
}


// Create relationship beteen vehicle and location

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



// Add date filtering for views

add_shortcode('ts_day_week', function($atts){
    $atts = shortcode_atts( array(
        'day' => 'Monday',
    ), $atts);
    $res = strtotime('last ' . $atts['day']);
    if(date('l') == $atts['day']){
        $res = strtotime('this ' . $atts['day']);
    }
    return $res;
});

?>

BTW - the select2 does not always work, is there something I'm doing wrong?

Thanks!

New threads created by Waqar and linked to this one are listed below:

https://toolset.com/fr/forums/topic/split-the-select2-does-not-always-work/

#1763419

Waqar
Supporter

Languages: Anglais (English )

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

I included the entire code from your last message into the "functions.php" file of my website's active theme and still couldn't break the website.

Did you get a chance to look for the errors and warnings as suggested in my last reply? That step is very important to narrow down to conflicting/problematic code.

As for the select2 not always working, I've split it into a separate ticket and will reply to that shortly.
( ref: https://toolset.com/forums/topic/split-the-select2-does-not-always-work/ )

#1763961

So you combined both the entire function in #1763023 AND added the code from #1761563 to it? the problem is that once they are combined (even with debug on I get that white screen error and I have to go into the db and switch to another theme to get back in (where I am I cannot access FTP because the port is blocked).

#1764173

Waqar
Supporter

Languages: Anglais (English )

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

As I suggested in my earlier reply ( ref: https://toolset.com/forums/topic/dynamic-field-values-from-cpt/page/2/#post-1762135 ), you'll need to make sure that the names of the function are not repeated or you'll get the error.

In the snippet from reply #1763023, you're using "func_to_dynamically_populate_vehicle_locations" name for populating options for the "Vehicle Location" field.
( screenshot: hidden link )

But then in the snippet from reply #1761563, the same "func_to_dynamically_populate_vehicle_locations" name is being used for the other function that populates options for the "Camera Title" field.
( screenshot: hidden link )

I guess in the snippet from reply #1761563, you rightly changed the function name to "func_to_dynamically_populate_camera_locations" in the first line, but missed it in the second one and used the same "func_to_dynamically_populate_vehicle_locations" which is causing the error.

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