Auto generate post title from two CPT meta fileds
-
Dear code experts on the forum, I really need urgent help for my website, I use CPT named ”auftragerstellen”, and do not fill in the post title(since the user could not fill in the title
on the frontend, or I could not manage to do it..), so we have the old classic problem, that every custom post type post has no title. You can not distinguish among them.
So I really need post title of custom post type ‘auftragerstellen’ being automatically created from two meta values’titel’ and ‘ort’
I found here a similar post https://wordpress.stackexchange.com/questions/94364/set-post-title-from-two-meta-fields
And I implemented it to my case:function set_auftrag_title( $data , $postarr ) { if ($data['post_type'] == 'auftragerstellen') { $auftrag_titel = get_post_meta( $postarr[ 'ID' ], 'titel', true ); $auftrag_ort = get_post_meta( $postarr[ 'ID' ], 'ort', true ); $auftrag_title = $auftrag_titel. ' in ' . $auftrag_ort; $post_slug = sanitize_title_with_dashes ($auftrag_title,'','save'); $post_slugsan = sanitize_title($post_slug); $data['post_title'] = $auftrag_title; $data['post_name'] = $post_slugsan; } return $data; } add_filter( 'wp_insert_post_data' , 'set_auftrag_title' , '10', 2 ); add_action( 'save_post', 'set_auftrag_title', 10, 3 );
The Problem is, the post title would be generated, after I have to update the post manually and not direct after the post is published or created. I know it is the limitation of this code, I searched many posts, and tried different codes, but I am newbie to php and wordpress and could not figure it out….. I tried last weekend and over hours but could not manage to implement the proper code… The custom post type is ‘auftragerstellen’ the two metal values should be ‘titel’ and ‘ort’ as in the above code, the post should has this format ‘titel’ in ‘ort’
I need the post title auto generated DIRECT after the post has been published, and no Post-update is needed.
Thank you very very much if you could help me solve this problem.. I tried many and nearly give up…
The page I need help with: [log in to see the link]
- The topic ‘Auto generate post title from two CPT meta fileds’ is closed to new replies.