• 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]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You want the post title auto generated after publishing, but don’t want an update? Posts have to be updated if already published in order to add the title, so that doesn’t make sense.

    Or do you mean the auto title routine is not required when the post is updated by the author or whomever? OK, that makes sense. Really what you want is to auto generate titles if there is no title defined, right? Whether it’s the initial publish (typical situation) or an update (existing posts with no title?), it really doesn’t matter.

    It shouldn’t even matter if it’s after publishing, or just before, as long as the data is present and the post is just about to be published. If true, the best filter to hook is ‘wp_insert_post_data’. Your filter callback can set the title here from available data just before post insertion by WP.

    Your callback should of course verify the post type is auftragerstellen. The post meta values should be in the $_POST array. They are keyed differently depending on the situation. Examine the array under different scenarios to be sure you have them all covered. If values are missing, your script could maybe check for meta values saved in the DB for values from which to compose a title. And even have a fallback default title in case there truly is no data. How about “This is not a title”? ??

    Of course, if the post title field is not empty, a title is already defined and your callback should return the passed data unchanged.

    Thread Starter minasato1

    (@minasato1)

    Dear Bcworkz,

    thanks for your quick reply. Your help is very appreciated.
    Thanks for the explaination. I unfortunately am just newbie to the wordpress coding…. I tried to understand the your words. But it is hard for me.
    Could you write me a code which could acompolish my purpose?
    The code above does work if you after pubishing UPDATE the post afterwards.
    but that is not I want, I want the title being set automatically after pubishing the costum post type, and no Need to update the post again.

    The customer post type is ‘auftragerstellen’
    the meta field of the auftragerstellen is respectively ‘titel’ and ‘ort’.
    The title should be like this ‘titel’ ‘in’ ‘ort’.
    This Problem could not be solved by me, due to lack of php and wordpress knowledge.
    I really appreciate if you could help me solve this Problem.
    Thank you very much in advance.

    Moderator bcworkz

    (@bcworkz)

    Ah! So that’s what you meant by not update. Yeah, that’s not good. Your actual goal should be to auto assign titles that are not defined during any post save action. Unfortunately, I cannot help you with the code for this. My apologies. Support here is principally for help in fixing coding issues more than developing anew. This topic will remain open for quite a while in case someone comes along who can help you with a freebie anyway. Never discuss payment terms in these forums.

    You can hire experienced coders at sites like jobs.wordpress.net or jetpack.pro . If you prefer a more personal relationship, you can also find coders at local WP meetups that happen in most large metro areas worldwide.

    Thread Starter minasato1

    (@minasato1)

    Dear Bcworkz,

    thanks for the quick reply. I am not English native speaker and do not know wordpress well(still learning..). So it might have caused some cofusion what I want to archive with the code…
    Thank you for the advices. I will look back to this site daily perhaps to see if there is any update here.
    Thanks for taking time answering my post:)

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! Sorry I couldn’t help more. You should not need to check back here for replies, but you may if you prefer. You should be getting e-mail notification of new replies at the address you supplied for your www.ads-software.com profile. There should be another notification due to this reply.

    Your English is very good! Communication between even two native speakers who don’t know each other can still get confused. That is the way it is, it’s not anyone’s fault ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Auto generate post title from two CPT meta fileds’ is closed to new replies.