[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.
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).
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.
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/
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;
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?
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.