I have updated the code snippet in your site and now it appears to be working good. I created a test post and the title is being generated using the 5 Custom Fields, you can find test post here: hidden link
Here is the updated code:
add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 ); // Grabs the inserted post data so you can modify it.
function modify_post_title( $data, $postarr )
{
//$post_id = $postarr['ID'];
if($data['post_type'] == 'landed-house' && isset($postarr['wpcf']) ) {
$field_1 = $postarr['wpcf']['property-type'];
$field_2 = $postarr['wpcf']['landed-house-storey'];
$field_3 = $postarr['wpcf']['landed-house-type'];
$field_4 = $postarr['wpcf']['property-address'];
$field_5 = $postarr['wpcf']['beds'];
$title = '';
if(isset($field_1) && $field_1 != '')
$title .= $field_1 . ' ';
if(isset($field_2) && $field_2 != '')
$title .= $field_2 . ' ';
if(isset($field_3) && $field_3 != '')
$title .= $field_3 . ' ';
if(isset($field_4) && $field_4 != '')
$title .= $field_4 . ' ';
if(isset($field_5) && $field_5 != '')
$title .= $field_5;
$data['post_title'] = $title ; //Updates the post title to your new title.
}
return $data; // Returns the modified data.
}
==> You can replace CPT slug and Custom fields slug in the above code as needed.
thank you Noman , its working but just one last question.
as you can see i have 6 similiar CPT , do you suggest create each CPT a custom function or combine all into one ?
combine has its issue too as the custom field that being used changes with the CPT. quite a messy i agree,
2. will there be any speed /resource issue by using this ?
i don't think any issue with the speed/resources as its created in backend. am i right to say this ?
btw FYI its not working in quick edit but does work in full edit.
great job done.
1. I would suggest to make copies of it and use it multiple times with separate function name, as you will also have different Custom Fields for each CPT.
Ok, I have marked this ticket as "Waiting" so that it does not stay in my working queue. The ticket will stay open for few days, once you confirm it for multiple CPTs, you can mark it as resolved.
For the other ticket, that is assigned to Luo and he will check it. I am going off from my daily shift now. So I hope Luo can get back to you earlier than I came back tomorrow 🙂
I tried to follow your instruction here but this didn't work!
I am trying to post Title using 4 Fields. I have added the code to Snippet plugin. but as i create a new post from admin the Title stays empty. if i create a a post from Cred the Title shows me ( CRED Auto Draft d0f8b201bbf07f2b377cf4b7b72ef75d )