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.
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
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.
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
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?
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