Skip Navigation

[Resuelto] Custom post title from 5 different custom fields.

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
Trying to auto-create post title base on 5 different custom fields.
Solution:
Custom solution here:
https://toolset.com/forums/topic/custom-post-title-from-5-different-custom-fields/page/2/#post-578300
Relevant Documentation:

This support ticket is created hace 7 años, 1 mes. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Etiquetado: 

Este tema contiene 31 respuestas, tiene 3 mensajes.

Última actualización por Nashaat hace 7 años, 1 mes.

Asistido por: Noman.

Autor
Mensajes
#578238

hi, i forgot to update you , the function is here

enlace oculto

thanks

#578300

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

#578304

thank you , does this work for existing post or new only ?

#578306

Noman
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Karachi (GMT+05:00)

It works for both: Existing posts and New posts.

#578307

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.

#578310

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.

#578311

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

#578317

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

#578318

Hi Noman , you may look at this ticket too if you have time :
https://toolset.com/forums/topic/frontend-sorting-using-custom-field-not-working/

thanks !

#578320

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

#578321

excellent , thank you. have a good rest.

#578322

Noman
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Karachi (GMT+05:00)

[ changing ticket status as = Waiting ]

#578783

THANKS

#579388

hi Noman, could you look at this ticket pls.

https://toolset.com/forums/topic/cpt-menu-url-format/

thanks

#897127
After-Posting-newPost.png

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;
}