Skip Navigation

[Resolved] Trying to create post title from 2 custom fields

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a CRED form that creates new posts. I would like to generate the title of each new post using information supplied in some custom fields from the CRED form.

Solution: Use the CRED API to modify the post title during the cred_save_data hook.

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

Relevant Documentation:
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

This support ticket is created 6 years, 10 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 8 replies, has 3 voices.

Last updated by Christian Cox 6 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#537796

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

#537986

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

#538677

Beda- thanks ever so much- thats exactly what i needed.

Steve

#538761

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?

#538775

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.

#538790
griffin.jpg

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

#539140

'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.

#539172

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

#539179

Great! I've learned something today 🙂

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