Skip Navigation

[Resolved] How to generate Post Title from custom field values?

This support ticket is created 5 years, 1 month 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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Himanshu Agarwal 5 years ago.

Assisted by: Waqar.

Author
Posts
#1220287

Hi,
I need to generate woocommerce product title from custom field value.

I tried solution explained here :-

https://toolset.com/forums/topic/create-post-title-for-cpt-from-two-custom-fields/

But, this solution is making site's add product down, it means after adding this code all site is working fine but when click on Products -> Add new, page is not opening.

Please help to solve this.

#1220741

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Himanshu,

Thank you for contacting us and I'll be happy to assist.

Using the "wp_update_post" function with the "save_post" hook can result in "infinite loop", so it is better to use "wp_insert_post_data" hooks in a case like this:
( ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data )

For example:


function tssupp_autogenerate_title( $data , $postarr ) {

	// check if the specific post type and not an auto-draft
	if ( ('product' == $postarr['post_type']) && ($data['post_status'] !='auto-draft') ) {

		// get the value of custom field
		$new_title = $_POST['wpcf']['slug-of-the-field'];

		// sanitize the title and the slug values 
		$new_title = sanitize_text_field( $new_title );
		$new_slug = sanitize_title( $new_title );

		// update the title and the slug values
		$data['post_title'] = $new_title;
		$data['post_name'] = $new_slug;

	}

	return $data;
}

add_filter( 'wp_insert_post_data', 'tssupp_autogenerate_title', '99', 2 );

Feel free to adjust this code snippet as needed and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1220743

I tried this code, but its not working.

I need to title auto generate when publish the post. I filled the custom field and leave the title field empty and hit the publish, this makes nothing to save, not title nor custom field value.

#1221353

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Himanshu,

The example code snippet from my last message would only work if a product is added/updated from the admin area and not through a Toolset Form.

Can you please make sure that you used a correct field slug in the code, in place of "slug-of-the-field"?

Since this code works on my test website, if the issue persists only on your website, it would be a good idea to test it with all non-Toolset plugins disabled (except WooCommerce) and a default theme like Twenty Nineteen.

regards,
Waqar

#1221370

Thank you Waqar,

Its working, but its require to fill default product title. I don't want to fill default title field, because I want to remove default title fields for all products, so title will be automatically generated on publish from custom field.

Is this possible to auto generate title for products without filling title field?

#1221632

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Himanshu,

This limitation seems to be coming from the WooCommerce plugin.

If you'll test the same code with any other post type and not the WooCommerce products, it will execute correctly, whether you'll fill the title field or not.

To overcome this, you have a couple of options:

1. You can consult WooCommerce's own support team to see if they have a suggestion to bypass their empty title check.

OR

2. You can include some custom script on the new product's edit screen, which can add some text in the title field on its own.

regards,
Waqar

#1222312

Thank you Waqar 🙂

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