Skip Navigation

[Resolved] Creating post title from a number of elements

This support ticket is created 3 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.

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

Author
Posts
#1657103

Tell us what you are trying to do?

Trying to create a post title for a child post from four elements ({Parent-post ID}{"-"}{date timestamp from new child post}{Custom field value from new child post}).

The Parent post "company profiles" already exists and this is to be inserted into the form creating the child post.
The Child post is "funding-events", the relationship is "company-profile_funding-event"
The date field is 'funding-event-date'
The other custom field is 'funding-amount'

I used the same title format to import a large number of child posts so as to ensure each had a unique title for importing purposes (using WPAllimport) so I now need to figure out how to create posts with the same title format.

I am not a programmer but have been able to figure out parts of the puzzle.

I know this is not quite right.. esp the parent post id and timestamp date, your assistance would be greatly appreciated.

add_filter('cred_save_data','combine_title');
function combine_title($post_id) {
$type = get_post_type($post_id);
if ($type == 'funding-event') {
$title .= $post_id . ' - ' . get_post_meta ($post_id, company-profile_id = $_POST['@company-profile_funding-event'] );
$title .= ' ' . get_post_meta($post_id, 'funding-event-date', true);
$title .= ' ' . get_post_meta($post_id, 'funding-amount', true);
$slug = sanitize_title($title)
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

Is there any documentation that you are following?

https://toolset.com/forums/topic/cred-saving-user-submitted-custom-field-as-post-title/#post-75805
https://toolset.com/documentation/programmer-reference/cred-api/
https://toolset.com/forums/topic/cred-creating-content/

Is there a similar example that we can see?

What is the link to your site?

hidden link

#1657143

Hi, I'll be glad to help. First, please note that all cred_save_data hooks you add in your custom code are triggered any time any Form is submitted. So as you add more Forms to the site, you need to accommodate for the fact that this code should only be triggered for specific Forms. Second, note that Types field slugs in the database have a wpcf- prefix. So if you're using get_post_meta to get the value of a custom field, you must use the wpcf- prefix with the slug. Also, if you want the parent post ID from the Form you shouldn't use get_post_meta, you should just get the value directly from the $_POST array, since it's not saved as a custom field. Also you don't need to put post ID in the title, based on your description of the desired custom title. So here is an update for you:

add_filter('cred_save_data','combine_title', 10, 3);
function combine_title($post_id, $form_data, $form_id) {
$forms = array( 123, 456 );
if ( in_array( $form_id, $forms ) ) {
  $title.= $_POST['@company-profile_funding-event'] ;
  $title .= ' ' . get_post_meta($post_id, 'wpcf-funding-event-date', true);
  $title .= ' ' . get_post_meta($post_id, 'wpcf-funding-amount', true);
  $slug = sanitize_title($title)
  wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
  }
}

You should change 123, 456 to be a comma-separated list of Form Ids where you want to apply this code. It may only be the one Form ID for the new child post Form, that's fine. You can find that ID in Toolset > Post Forms.

Try this and let me know the result.

#1657221

Christian

Thanks so much for your prompt reply.

I have tried what you suggested but unfortunately it doesn't seem to do anything. I am also wanting to add " - " between the first two elements of the post title.

Here is what I used:

//to set the post title for funding events
/*
add_filter('cred_save_data','combine_title', 10, 3);
function combine_title($post_id, $form_data, $form_id) {
$forms = array( 62129 );
if ( in_array( $form_id, $forms ) ) {
$title.= $_POST['@company-profile_funding-event'] ;
$title .= ' ' . get_post_meta($post_id, 'wpcf-funding-event-date', true);
$title .= ' ' . get_post_meta($post_id, 'wpcf-funding-amount', true);
$slug = sanitize_title($title)
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}

can you suggest any changes?

Cheers

Peter

#1657255

Okay check the second line here:

//to set the post title for funding events
/*
add_filter('cred_save_data','combine_title', 10, 3);

The /* characters start a multi-line comment in PHP, and the characters */ end a multi-line comment. The code between those special indicators is not executed.

/*
all this 
code is
commented
and will not
be executed
*/

A single line comment is like you have in the first line, beginning with two slash marks //. So you should definitely remove the /* line, and if there is a closing comment string */ somewhere lower in the code, remove that too. Then I can see that you are missing a closing } bracket, so I have added it back here:

//to set the post title for funding events
add_filter('cred_save_data','combine_title', 10, 3);
function combine_title($post_id, $form_data, $form_id) {
$forms = array( 62129 );
if ( in_array( $form_id, $forms ) ) {
$title.= $_POST['@company-profile_funding-event'] ;
$title .= ' ' . get_post_meta($post_id, 'wpcf-funding-event-date', true);
$title .= ' ' . get_post_meta($post_id, 'wpcf-funding-amount', true);
$slug = sanitize_title($title)
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
}
}

If this isn't doing anything, feel free to provide login credentials in the private fields here and I can take a closer look. Let me know where I can find the Form on the front-end of the site.

#1657419

I see, you're missing a semicolon at the end of this line:

$slug = sanitize_title($title)

Should be:

$slug = sanitize_title($title);

I'm not able to access the wp-admin area using this login, unless I'm missing something? Let me know how to proceed.

#1657751
Annotation 2020-06-11 094753.png

Christian

I have revised the login details ... sorry about that.

I did try the change to the code you suggested.. although it did not work.. post saved but no change to the title... and I got an error message after submitting the post - see attached.

The form can be accessed from within company profiles, by an admin... under the company logo... just press the create new funding event button... they can be accessed from the directory which is available under the startup data tab.

hope you can help me figure this one out.

cheers

Peter

#1658697

Okay got it, I made a few adjustments and now the new posts have modified titles. Here is the updated code in your child theme's functions.php file:

add_filter('cred_save_data','combine_title', 10, 2);
function combine_title($post_id, $form_data) {
$forms = array( 62129 );
if ( in_array( $form_data['id'], $forms ) ) {
  $title = $_POST['@company-profile_funding-event_parent'] ;
  $title .= ' ' . get_post_meta($post_id, 'wpcf-funding-event-date', true);
  $title .= ' ' . get_post_meta($post_id, 'wpcf-funding-amount', true);
  $slug = sanitize_title($title);
  wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_name' => $slug));
  }
}

Now the new post titles have this format:
46679 1591056000 100000

Please check the Form and let me know if it's not working as expected.

#1659345

Thanks that worked perfectly... just made some minor adjustments for the format of the title.

I have a few other issues around the move to the new relationships structure but will open new threads for those.

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.