Skip Navigation

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

This support ticket is created 6 years, 9 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.

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, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#543129

Hello,

I previously had help setting up custom post types to generate their own post titles.

I have since modified how forms are filled in and need to have custom post types generate their post titles depending on what is filled out in the form.

I have a form at hidden link

When a new member is filling it out, they choose either Individual or Group.

I need the CPT that is created to use either

Member First Name / Member Last Name

Or

Member Group Name

as the CPT title.

This it the code previously developed with Toolset:

add_filter('cred_save_data','item_title');
function item_title($post_id, $form_data) {
$type = get_post_type($post_id);
if ($type == 'member') {
$c1 = get_post_meta($post_id, 'wpcf-member-first-name', true);
$c2 = get_post_meta($post_id, 'wpcf-member-last-name', true);

$title= $c1. ' ' . $c2;

$slug = sanitize_title($title);
$args = array('ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
if ($type == 'donation') {
$c1 = get_post_meta($post_id, 'wpcf-member-first-name', true);
$c2 = get_post_meta($post_id, 'wpcf-member-last-name', true);

$title= $c1. ' ' . $c2;

$slug = sanitize_title($title);
$args = array('ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
if ($type == 'material-checkout') {

$parent_id = $_POST["_wpcf_belongs_member_id"];
$title = get_the_title($parent_id);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status' => 'publish'));
}
if ($type == 'check-in') {

$parent_id = $_POST["_wpcf_belongs_member_id"];
$title = get_the_title($parent_id);
$slug = sanitize_title($title);
wp_update_post(array('ID' => $post_id, 'post_title' => $title, 'post_status' => 'publish'));
}
}

function changenotset($current_options, $title, $type) {
foreach ($current_options as &$option) {
if ($option['#title']=='--- not set ---') {
$option['#title'] = "--- Choose ---";
}
}
return $current_options;
}
add_filter("wpt_field_options", 'changenotset', 10, 3);

Please advise how I might do this.

Thanks.

#543296

Dear Arrien,

It is a custom codes problem, I suggest you try these:
1) Use CRED action hook "cred_save_data" to trigger a custom PHP function when your user submit the specific CRED form:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
2) In your custom PHP function, do these step by steps:
a) Get the value field "Member type"
https://codex.wordpress.org/Function_Reference/get_post_meta
b) if it is a Individual, then update the post title to what you want:
https://developer.wordpress.org/reference/functions/wp_update_post/
c) Similar as b) if it is a Group, update the post title to what you want.

#544834

Hi Luo,

Sorry, this is very overwhelming for me.
I am not a PHP coder.
Can you give me a bit more guidance as to where to start?

Thanks.

#544922

I have already provide detail steps for you in above post:
https://toolset.com/forums/topic/custom-post-types-generate-their-own-post-titles-from-1-or-2-custom-fields/#post-544834

If you need more assistance for it, please provide a test site with same problem, fill 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 the custom PHP codes

#546450

Hi Luo,

For someone who doesn't write PHP, as I posted earlier, the steps you provided could only get me so far.

I tried this and it seems to work, but I'm not sure how to make it specific for Individuals or Groups.

//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) {
if ($form_data['id']==8) {
$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;
$args = array('ID' => $post_id, 'post_title' => $title);
wp_update_post($args);
}
}

How do I go from here?

#546712

You did not do as I mentioned above:
https://toolset.com/forums/topic/custom-post-types-generate-their-own-post-titles-from-1-or-2-custom-fields/#post-543296
2) In your custom PHP function, do these step by steps:
a) Get the value field "Member type"
https://codex.wordpress.org/Function_Reference/get_post_meta
b) if it is a Individual, then update the post title to what you want:
https://developer.wordpress.org/reference/functions/wp_update_post/
c) Similar as b) if it is a Group, update the post title to what you want.

How do you setup the "Member type"? is it a custom field?
Then you can simply get the feild "Member type" value with wordpress function get_post_meta()

#548398

Like this?

//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title');
function func_custom_post_title($post_id,$form_data) {
if ($form_data['id']==8) {
$membertype = get_post_meta($post_id, 'wpcf-member-type', true);
wp_update_post($args);
}
}

#548585

In your PHP codes:
$membertype = get_post_meta($post_id, 'wpcf-member-type', true);
This line can get he "member-type" value, then you will check the value:
if it is a Individual, then update the post title to something you wanted
hidden link
if it is a Group, update the post title to something else

Since it is a custom PHP codes problem , if you still need assistance for it, you can provide a database dump file of your website, also point out the problem page URL and CRED form URL, I can setup an example PHP code for you

#549545

Hi Luo,

I will try to figure out what you suggest.
I am not a PHP coder, so hopefully I can make it work.

#549550

Hi Luo,

I tried this, but it doesn't work:

<?php
/**
* @package Make Child
*/

/**
* The theme version.
*/
define( 'TTFMAKE_CHILD_VERSION', '1.1.0' );

/**
* Turn off the parent theme styles.
*
* If you would like to use this child theme to style Make from scratch, rather
* than simply overriding specific style rules, simply remove the '//' from the
* 'add_filter' line below. This will tell the theme not to enqueue the parent
* stylesheet along with the child one.
*/
//add_filter( 'make_enqueue_parent_stylesheet', '__return_false' );

/**
* Add your custom theme functions here.
*/

//Save Member name as post title.
add_action('cred_save_data','func_custom_post_title');
function func_custom_post_title($post_id,$form_data) {
$membertype = get_post_meta($post_id, 'wpcf-member-type', true);
if ($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;

$slug = sanitize_title($title);
$args = array('ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
if ($if ($type == 'group') {
{
$groupname = get_post_meta($post_id, 'wpcf-member-group-name', true);

$title= $groupname. ' ' . ;

$slug = sanitize_title($title);
$args = array('ID' => $post_id,
'post_title' => $title,
'post_name' => $slug
);
wp_update_post($args);
}
}

#549677

Since it is a custom PHP codes problem and you did not provide the website credentials, could you provide a database dump file of your website? I can duplicate same problem and debug it in my localhost, thanks

#550391

Hi Luo,

I was trying to understand how to write the code.
Can I provide the website credentials now?

#550591

OK, the private detail box is enabled, please provide the credentials and FTP access, also point out the problem page URL and CRED form URL, and where I can edit your PHP codes, I need to test and debug it in a live website. thanks

#551158

Thanks for the details, I have modified your PHP codes as below:

//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) {
    if ($form_data['id']==8) {
        $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);
		}
    }
}

Please test again, check if it is what you need. thanks

#551870

Hi Luo,

Thank you.

It was working until I tried to make the form work with for ID 70 as well.

//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) {
if ($form_data['id']==8) {
$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);
}
}
if ($form_data['id']==70) {
$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);
}
}
}

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