Skip Navigation

[Resolved] Custom post types generate their own post titles from 1 or 2 custom fields

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 29 replies, has 2 voices.

Last updated by Arrien 6 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#551889

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

#552076

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

#552320

This line:

arr = array(8, 70); // here you can add more CRED form IDs

Should be:

$arr = array(8, 70); // here you can add more CRED form IDs

There is a "$" missed.

#552610

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

#552824

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/

#553939

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

#554529

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

#554869

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.

#554872

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

#554974

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

#555471

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!

#555654

Hi Luo,

I have modified the FTP info so you should have access now.

Thanks.

#555700

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.

#555884

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.

#556123

Hi Luo,

Thank you.

Using "Choose from previously created fields" worked great.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.