hi, i forgot to update you , the function is here
enlace oculto
thanks
Noman
Supporter
Idiomas:
Inglés (English )
Zona horaria:
Asia/Karachi (GMT+05:00)
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: enlace oculto
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.
I hope this resolves the issue, Thanks
thank you , does this work for existing post or new only ?
Noman
Supporter
Idiomas:
Inglés (English )
Zona horaria:
Asia/Karachi (GMT+05:00)
It works for both: Existing posts and New posts.
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 ?
thanks.
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.
Noman
Supporter
Idiomas:
Inglés (English )
Zona horaria:
Asia/Karachi (GMT+05:00)
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.
2. Yes you are right, I don't think there is any speed issue, 'wp_insert_post_data' is just a filter hook provided by WordPress itself:
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
As such, this approach is safe to use. Thanks
Thank you for your quick reply.
If you dont mind, ill keep this ticket open for couple of days for the other cpt then close
Noman
Supporter
Idiomas:
Inglés (English )
Zona horaria:
Asia/Karachi (GMT+05:00)
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 🙂
[ changing ticket status as = Waiting ]
Thanks
excellent , thank you. have a good rest.
Noman
Supporter
Idiomas:
Inglés (English )
Zona horaria:
Asia/Karachi (GMT+05:00)
[ changing ticket status as = Waiting ]
Hi Noman,
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 )
This is my code:
add_filter ('title_save_pre','combine_title');
function combine_title($title) {
global $post;
$type = get_post_type($post->ID);
if ($type == '0km') {
$field_1 = get_post_meta($post->ID, 'wpcf-car-manufacturer', true);
$field_2 = get_post_meta($post->ID, 'wpcf-car-name', true);
$field_3 = get_post_meta($post->ID, 'wpcf-car-version', true);
$field_4 = get_post_meta($post->ID, 'wpcf-model-year', true);
$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 . ' ';
}
return $title;
}