Im trying to write a function that concatenates two custom fields into the post title- Im not really a programmer but have put together this function (that doesnt work at all) based on some similar issues and resolutions ive seen here. Could someone with better php skills help me out with this- thanks in advance
function griffin_modify_title($title) {
if ( get_post_type( $id ) == 'chervinsky-app' ) {
$first = types_render_field('ca-first-name', array('raw'=>'true') );
$last = types_render_field( 'ca-last-name', array('raw'=>'true') );
$title = $first.' '.$last;
}
return $title;
}
add_filter('the_title', 'griffin_modify_title', 10, 2);
I can not assist this code, as it does not use any Toolset API but only WordPress API.
You seem to want to update the Post Title with some Custom Fields values.
This is done with the wp_update_post() hook.
You can do that either in a CRED cred_save_data() hook (if you submit the post in a CRED form), or in the save_post() hook (if you want this to happen in the backend when updating/creating the post)
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://codex.wordpress.org/Function_Reference/wp_update_post
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
You can get the value of the Custom Fields with the WordPress API:
https://developer.wordpress.org/reference/functions/get_post_meta/
Here is a fully working example of the exact task you want to accomplish, using the CRED hook:
https://pastebin.com/2BTbVbcs
Beda- thanks ever so much- thats exactly what i needed.
Steve
Well i spoke too soon- when i tested on my local host it worked great- but when i put in place in the live site whats happening is that the form takes long enough to complete that wordpress has autosaved the post and the title becomes something like
CRED Auto Draft f4dd0a59c4748b8af6d68653d41728c2
Thoughts?
Hi, Beda is unavailable today. I've reviewed the information here and I don't see any reason for concern related to this code and default title. It doesn't matter if the post has already been autosaved, or if a default title is saved for that draft post in the database. When you run my_save_data_action during the cred_save_data hook, it will overwrite whatever temporary title was put in place with the values you define for post_title and post_name, respectively.
Thanks Christian- Ive just done another test with the same results. You can see the post names in the screenshot below. I did modify the original function a bit to include the fields i wanted and it is now
function sf_save_data_action($post_id, $form_data){
// Change your CRED Form "ID" accordingly below
if ($form_data['id']==8096){
//Declare the content of your variables, change "your_custom_field_slug" accordingly
$first = get_post_meta( $post_id, 'wpcf-ca-first-name', true );
$last = get_post_meta( $post_id, 'wpcf-ca-last-name', true );
$custom_title = $last.' '.$first;
//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title,
'post_name' => $custom_title,
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_action('cred_save_data', 'sf_save_data_action',10,2);
Anything jump out at you
'post_name' => $custom_title
This might be an issue because your new custom title isn't formatted for use as a slug - there's a space in the title, which is not allowed in slugs. As a test, temporarily remove this item from the update array and try again. Let's see if the title is updated as expected, then we can take another look at the post_name (slug). Try it like this:
...
...
$my_post = array(
'ID' => $post_id,
'post_title' => $custom_title
);
...
...
If this still does not update the title of your post, we'll need to enable server-side logging. You can see how to enable debug logging here:
https://toolset.com/documentation/user-guides/debugging-toolset/
Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:
define('WP_DEBUG', true);
Then add these lines, just before it says 'stop editing here':
ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
ini_set('display_errors', 'Off');
Submit the CRED form again. If any server-side errors are thrown during the process, this will create an error_log.txt file in your site's root directory. Please send me its contents. Once that is done, you can revert the updates you made to wp-config.php.
Christian thanks again- Im so sorry but the issue was mine I had uploaded the wrong copy of functions.php after i tested on my dev server...
And for future reference the slug comes out fine wordpress re-writes it and turns spaces into dashes and removes the disallowed characters
Great! I've learned something today 🙂