You can modify the codes I provided in post:
https://toolset.com/forums/topic/custom-post-types-generate-their-own-post-titles-from-1-or-2-custom-fields/#post-551158
This line from:
if ($form_data['id']==8) {
To:
$arr = array(8, 70); // here you can add more CRED form IDs
if (in_array($form_data['id'], $arr)) {
More help:
hidden link
in_array — Checks if a value exists in an array
Hi Luo,
I did this and it broke the site:
//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title', 10, 2);
function func_custom_post_title($post_id,$form_data) {
arr = array(8, 70); // here you can add more CRED form IDs
if (in_array($form_data['id'], $arr)) {
$type = get_post_meta($post_id, 'wpcf-member-type', true);
$title = '';
if ($type == 'individual') {
$firstname = get_post_meta($post_id, 'wpcf-member-first-name', true);
$lastname = get_post_meta($post_id, 'wpcf-member-last-name', true);
$title = $firstname. ' ' . $lastname;
}
if ($type == 'group') {
$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);
$title = $groupname;
}
if($title){
$slug = sanitize_title($title);
$args = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
}
}
Hi Luo,
When I fill out the Donation form at hidden link
I still get Donations saving as CRED Auto Draft ...
Is it because the individual and group were capitalized Material Donation Fields?
Thanks
Since the original question of this thread has been resolved, please create new thread for the new question, that will help other users to find the answers.
https://toolset.com/forums/topic/custom-post-types-generate-their-own-post-titles-from-1-or-2-custom-fields/
Hi Luo,
I understand.
Just to be sure though, if the custom post fields are all correct, the following should create Post Titles using the Member name, right?
//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title', 10, 2);
function func_custom_post_title($post_id,$form_data) {
$arr = array(8, 70); // here you can add more CRED form IDs
if (in_array($form_data['id'], $arr)) {
$type = get_post_meta($post_id, 'wpcf-member-type', true);
$title = '';
if ($type == 'individual') {
$firstname = get_post_meta($post_id, 'wpcf-member-first-name', true);
$lastname = get_post_meta($post_id, 'wpcf-member-last-name', true);
$title = $firstname. ' ' . $lastname;
}
if ($type == 'group') {
$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);
$title = $groupname;
}
if($title){
$slug = sanitize_title($title);
$args = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
}
}
Yes, it should be able to update the post title as what you want. if you still need assistance for it, please describe detail steps for how and where I can see the problem, I need to test and debug it in a live website, thanks
Hi Luo,
I have setup a new environment at hidden link
I've tested both these post forms:
hidden link
hidden link
And both of them create CRED Auto Draft post titles.
Please advise.
Nevermind, the only one that is not working and creating CRED Auto Draft post titles is:
hidden link
This is the code that I am using:
//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title', 10, 2);
function func_custom_post_title($post_id,$form_data) {
$arr = array(12, 13); // here you can add more CRED form IDs
if (in_array($form_data['id'], $arr)) {
$type = get_post_meta($post_id, 'wpcf-member-type', true);
$title = '';
if ($type == 'individual') {
$firstname = get_post_meta($post_id, 'wpcf-member-first-name', true);
$lastname = get_post_meta($post_id, 'wpcf-member-last-name', true);
$title = $firstname. ' ' . $lastname;
}
if ($type == 'group') {
$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);
$title = $groupname;
}
if($title){
$slug = sanitize_title($title);
$args = array(
'ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
}
}
Since it is a custom PHP codes problem, please provide the credentials of your new test site in below "private detail box", I need to test and debug it in a live website, thanks
Thanks for the details, I can login your website, but the FTP access isn't valid, I see empty folder when login with the FTP access, please check it.
Since the FTP access is not valid, so I can only edit the theme file functions.php in wordpress admin side, when I put the PHP codes into it, it triggers PHP 500 error, it might conflict with the plugin "CUCCR Membership System / Add-on" you mentioned above, but the FTP access is not valid, so I can not restore it back, please check the FTP access, make sure it is valid, thanks!
Hi Luo,
I have modified the FTP info so you should have access now.
Thanks.
Hi Luo,
I think I know what the problem is.
The slug for Membership fields and Material Donation Fields are slightly different, since we cannot have the same slugs for different field groups.
Membership fields:
member-first-name
member-last-name
member-group-name
Material Donation Fields:
member-first-name-donor
member-last-name-donor
member-group-name-donor
Is there a way to use the same slug for these two field groups so that the function in functions.php works?
In a live install at cuccr.ca that somehow uses the same slug between two field groups.
Let me know what is possible.
Thanks.
I think there is a misunderstand, I assume we are talking about custom field slug "member-first-name", it isn't custom field group slug.
And you can add same custom field into two different custom field group, for example, you can try this:
edit the custom field group '"Material Donation Fields",
hidden link
click button " Add New Field", click button "Choose from previously created fields", find the custom fields you needed, and put it into your custom field group.
And please create new thread for the new question, that will help other users to find the answers.
Hi Luo,
Thank you.
Using "Choose from previously created fields" worked great.