Skip Navigation

[Resolved] Autosave title from CFand add incremental number starting from 1 each year

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

Problem:
The issue here is that the user wanted to auto assign their post title based on the custom field values on their Post Form.

Solution:
This can be done by using the code below. Add the following to your Toolset custom code settings at Toolset->Settings->Custom Code. Once you've done this please ensure that you've activated it as well as change the ID 123 to the ID of your view.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==123) {
$field_1 = $_POST['wpcf-field-slug-1'];
$field_2 = $_POST['wpcf-field-slug-2'];
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);
}
}

The above is a basic method on how to update the post title.

This support ticket is created 3 years 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 5 replies, has 2 voices.

Last updated by felixM-3 3 years ago.

Assisted by: Shane.

Author
Posts
#2262667

Hello,

I have a Custom post type "lucrari" with a custom field "tip". I use Toolset Forms to create new custom post "lucrari".

I want when i create a new custom post "lucrari" to autosave title on Form submit. The title must be concatenated from auto-increment number + custom field ("tip") + curent year, something like 1-tip-2022, 2-tip-2022, etc (next year i want that the auto-increment number to start again from 1 - 1-tip-2023, 2-tip-2023, etc).

Please help me to achieve this.

Thank you!

#2262823

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felix,

Thank you for getting in touch.

This can be done by using the code below. Add the following to your Toolset custom code settings at Toolset->Settings->Custom Code. Once you've done this please ensure that you've activated it as well as change the ID 123 to the ID of your view.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==123) {
$field_1 = $_POST['wpcf-field-slug-1']
$field_2 = $_POST['wpcf-field-slug-2']
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);
}
}

This is how you will modify the post title, however the auto incremented number will be a bit difficult to achieve since you will be doing it based on a previous posts title.

It would require that you splice the title of the previous post to get the numeric value and then increment that numeric value by one. This can get a bit complex as you can see from the document below.
hidden link

My recommendation is that you contact one of the contractors on our contractors page for them to implement this custom solution for you.
https://toolset.com/contractors/

Thanks,
Shane

#2263553

Hi Shane,

Thank you for your support.
My question is something like this will work:

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

// Get the most recent post
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'you custom post type',
'post_status' => 'publish',
'year' => $getdate["year"], // this will select only posts from current year?

);
$last_post = wp_get_recent_posts($args);
// Get the title
$last_post_title = $last_post['0']['post_title'];
// Get the title and get the number from it. + increment from that number
$number = explode('-', $last_post_title); // the title format is 1-tip-2022
$number = $number[1] + 1;

if ($form_data['id']==123) {
$field_1 = $_POST['wpcf-field-slug-1']
$field_2 = $_POST['wpcf-field-slug-2']
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);
}
}

Please help me to put this together in the right way to figure out a solution. If the get most recent post from current year will return no post the increment number will be 1?

Thank you!

#2263703

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Felix,

Try this below.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

// Get the most recent post
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'you custom post type',
'post_status' => 'publish',
'year' => $getdate["year"], // this will select only posts from current year?

);
$last_post = wp_get_recent_posts($args);
// Get the title
$last_post_title = $last_post['0']['post_title'];
// Get the title and get the number from it. + increment from that number


if ($form_data['id']==123) { // Checks for correct form ID
$field_1 = $_POST['wpcf-field-slug-1']; // Gets value from custom field.
if (!empty($last_post)){ // Checks if the last post exist and if it does auto increment the title.
$number = explode('-', $last_post_title); // the title format is 1-tip-2022
$number = $number[0] + 1;
$args = array('ID' => $post_id, 'post_title' => $number.'-'.$field_1.'-'.date("Y"));
wp_update_post($args);
}else{
$args = array('ID' => $post_id, 'post_title' => '1-'.$field_1.'-'.date("Y"));
}
}
}

This should be able to help you. Please not that you will need to change wpcf-field-slug-1 to the slug of the custom field that you will want to get the value from.

Thanks,
Shane

#2264359

Hello Shane,

Thank you very much for your help. I figured out the final solution for me that work great. I post here my function, maybe somebody else will need it.

add_action( 'cred_save_data', 'increment_lucrare_id', 10, 2);
function increment_lucrare_id( $post_id, $form_data ){
    $form_id = 2129; // Edit CRED form ID
    $custom_field = 'l-uid'; // Edit the slug of the custom field to increment
    if ( $form_id == $form_data['id'] ) {
        // Find the current highest counter
				$getdate = getdate();
            $args = array(
                     'post_type' 			=>	'lucrare',
                    'post_status'   		=> 	'publish',
                    'date_query' 			=> 	array(
		                    'year'  => 	$getdate["year"],
			                    ),
                    'meta_key'  			=>   'wpcf-' . $custom_field,
                    'orderby'  			=>   'meta_value',
                    'order'     				=>   'DESC',
                    'posts_per_page'        =>   1
        );
        $posts = get_posts( $args );
        if ( !empty( $posts ) ) {
            $counter = get_post_meta( $posts[0]->ID, 'wpcf-' . $custom_field, true );
        } else {
            $counter = 0;
        }
        $counter ++;
        update_post_meta( $post_id, 'wpcf-' . $custom_field, $counter );
                    $field_ctg = $_POST['wpcf-l-ctg'] ;
                    $lid = $counter.'-'.$field_2.'-'.date("Y") ;
                    update_post_meta( $post_id, 'wpcf-l-id', $lid );
                    $ptitle = array('ID' => $post_id, 'post_title' => $counter.'-'.$field_ctg.'-'.date("Y"));
                    wp_update_post($ptitle);
    }
}
#2264361

My issue is resolved now. Thank you!