I am trying to: Auto create and change post title from author name and post last modified date
Link to a page where the issue can be seen: hidden link
I expected to see: the post name as the "[author name] Net Worth for [last modified date]"
Instead, I got: CRED Auto Draft c5f5efeb8395cb8a8233ffc966d4e136
I have looked at the following posts:
https://toolset.com/forums/topic/cred-set-post-title-and-author-email-cred_submit_complete/
https://toolset.com/forums/topic/auto-create-post-title/
https://toolset.com/forums/topic/cred-auto-create-post-title-2/
https://toolset.com/forums/topic/auto-create-post-title-2/
https://toolset.com/forums/topic/cred-how-to-set-post-title-automatically-through-custom-fields/
https://toolset.com/forums/topic/generate-the-post-title-in-cred-from-the-values-of-3-custom-fields-in-the-form/
but couldn't get this done.
I use the code below to get last modified date as a short code (and it works as a short code):
// display post last modified date
add_shortcode('wpv-post-modified', 'wpv_post_modified_shortcode');
function wpv_post_modified_shortcode($atts) {
return get_the_modified_date($atts['format']);
}
and the following code to try and set the post title automatically when the post is created or when its updated:
add_action('cred_save_data', 'build_title', 10, 2);
function build_title($post_id, $formdata)
{
$author=get_post_meta($post_id, 'wpv-post-author');
$mesg= " Net Worth for: ";
$modified_date = get_post_meta($post_id, 'wpv-post-modified');
$post_title="$mesg $author $modified_date";
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title));
}
please advise
thanks,
David
Dear David,
It is a custom PHP codes problem, I suggest you follow our document to setup your PHP codes:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
cred_save_data
This hook allows doing a custom action when post data is saved to database.
And you can get the example codes by clicking link "Usage examples"
If you still need assistance for it, please provide a test site with same problem, and fill below "private detail box" with login details and ftp access, also point out the problem page URL, and CRED form URL, where I can edit your PHP codes, I need a live website to test and debug, thanks
The function get_post_types() will be able to retrieve all post types as an array:
https://developer.wordpress.org/reference/functions/get_post_types/
In your case, I think you need another function get_post_type().
For example you can modify the PHP codes from:
$post_types = get_post_types( $args, $output, $operator );
To:
$post_type = get_post_type( $post_id );
More help:
https://developer.wordpress.org/reference/functions/get_post_type/
Retrieves the post type of the current post or of a given post.
Hi Luo Yang,
great, changing the code did the trick.
one last question in regard to this issue. I noticed that the post title and the post Permalink are not the same
is there a way to have the title as part of the Permalink and replace the random value 'cred-auto-draft-c5f5efeb8395cb8a8233ffc966d4e136-8'?
current Permalink: hidden link
desired Permalink: hidden link slug]
Thanks,
David
For the post Permalink, you will need to update another database table column "post_name", for example, you can modify your PHP codes, this line from:
$args = array('ID' => $post_id, 'post_title' => $title);
To:
$slug = sanitize_title($title);
$args = array('ID' => $post_id, 'post_title' => $title, 'post_name'=>$slug);
More help:
https://codex.wordpress.org/Function_Reference/wp_update_post
thank you so much Luo Yang,
this solve the issue.
Great support,
David