I have a function that is supposed to grab the custom field and use that as the post title:
// Dynamically populate existing VIN data for existing vehicle update
add_filter( 'wpt_field_options', 'func_to_dynamically_populate_vin', 10, 3);
function func_to_dynamically_populate_vin( $options, $title, $type ){
switch( $title ){
case 'existing vin':
$options = array();
// add first empty value
$options[] = array('#value' => '', '#title' => '---');
// get all VIN post items
$args = array( 'post_type' => 'vehicle', '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;
}
What am I missing here? The 'vin' field is supposed to write to the post title. Thanks!
Hello,
I have tried the same codes you mentioned above in my localhost, it works fine, please check these in your website:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/
2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2020, deactivate all custom PHP/JS code snippets, and test again
3) Also check if there is any PHP/JS error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
4) If the problem still persists, please provide database dump file(ZIP file) of your website, you can put the package files in your own google drive disk, share the link only, also point out the problem page URL, I need to test and debug it in my localhost, thanks
https://toolset.com/faq/provide-supporters-copy-site/
Actually, what I did was the wrong function altogether so technically the function was fine but it wasn't doing what I wanted it to do.
Here is what I have so far:
//Force title for Add Record from VIN field.
add_action('cred_save_data', 'build_vin_title', 10, 2);
function build_vin_title($post_id, $form_data) {
$forms = array( 8712,6944,12486,942,937 );
if ( in_array($form_data['id'], $forms) ){
$vin = get_post_meta($post_id, 'wpcf-vin', true);
$locale = get_post_meta($post_id, 'wpcf-vehicle-location', true);
$post_title=$vin. ' ' .$locale ;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}
What is happening now is the title is being replaced (it was simply saying CRED - blah blah blah) with just the post id #.
Thanks!
I have tried your custom PHP codes in my localhost, it works fine in my theme file "functions.php", please make sure you have put it in your theme file "functions.php", and test again
As I mentioned above, it works fine in my localhost, please check these:
1) Make sure you are using the latest version of Toolset plugins, you can download them here:
https://toolset.com/account/downloads/
2) In case it is a compatibility problem, please deactivate all other plugins, and switch to wordpress default theme 2020, deactivate other custom PHP snippets, and test again
3) Also check if there is any PHP error in your website:
https://toolset.com/documentation/programmer-reference/debugging-sites-built-with-toolset/
I don't see how this could be a compatibility problem if I have other functions that do exactly the same thing.
Please provide a full copy of your website in below private message box, also point out the problem page URL and form URL, where I can edit your PHP codes, thanks
Wouldn't it be easier (and safer) just to give you access to the site?
Hi,
Luo is on vacation, so I'll be following up on this ticket.
I already have the admin access to your website, but can you please share the link to a page where this form can be seen and tested?
regards,
Waqar
Sure, thanks Waqar,
You can see the posts here - /wp-admin/edit.php?post_type=vehicle
The cred is here - /wp-admin/post.php?post=8712&action=edit&classic-editor
The form is here - /add-car/
The results display on the home page and here - /garage-counts/ (BTW - you can see that because it stopped working there is a test post missing the 'vin' because it pulls from the title)
This might just be something silly that I missed. But I tried redoing it several times.
Thank you for sharing these details.
I tested the form and noticed that the ID of the post selected in the "Vehicle Location" field is correctly being used to create the new post's title.
( please check the newly created Vehicle post "6927" )
As for the Vin part, that is not being included in that title, that is due to another function, in the theme's "functions.php" file
add_action('cred_save_data', 'copy_location_to_multiline_field',10,2);
function copy_location_to_multiline_field($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==8712)
{
if (isset($_POST['wpcf-vehicle-location']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-vin', $_POST['wpcf-tmna-location']);
}
}
}
This function sets the empty value to the "wpcf-vin" field, as this form has no "wpcf-tmna-location" field.
As a result, when the "build_vin_title" executes the "wpcf-vin" field already has the empty value set and so no vin is included in the title.
My issue is resolved now. Thank you!