map the value of custom field into wp default post_titel

Support MB Builder map the value of custom field into wp default post_titelResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #29656
    Ole PoetterOle Poetter
    Participant

    Hi,

    I'm searching for hours.

    I have a custom field id=headline in my fieldgroup.
    The value of that field I want to put in the wp default post_title.

    How can I get this?
    I tried different solutions form the forum here.
    But either the title is "(No-Title)" or "automatically saved draft".

    Can you give me please a step-by-step guide?

    Kind regards,
    Ole

    #29661
    RogerRoger
    Participant

    So if i stand correctly, you want the field with the name Headline to be the post title?
    If so.

    Edit your post type within metabox
    Go to Supports
    Make sure Title is turned of

    Then create a code snippit (download first a snippit plugin)

    
    function change_post_title_by_field_value( $post_id ) {
        if ( get_post_type( $post_id ) == 'YOUR_CUSTOM_POST_TITLE_HERE') {
            $headline_title= rwmb_meta( 'headline', $post_id );
            $title = $headline_title;
            $title = sanitize_text_field( $title );
            $new_slug = sanitize_title( $title );
        } else {
            return;
        }
        
        // prevent a loop
        remove_action('save_post', 'change_post_title_by_field_value');
        
        $content = array(
            'ID' => $post_id,
            'post_title' => $title,
            'post_name' => $new_slug,
        );
        wp_update_post($content);
        
        add_action('save_post', 'change_post_title_by_field_value');
        
    }
    add_action('save_post', 'change_post_title_by_field_value');
    
    #29662
    Ole PoetterOle Poetter
    Participant

    Hi Roger,

    thanks for reply.

    that seems not to work.

    The post_title is saved as "Auto Draft" not with the text in the field "headline"

    Kind regards,
    Ole

    #29663
    RogerRoger
    Participant

    what is the name from your custom post type?

    #29664
    Ole PoetterOle Poetter
    Participant

    I edited my reply....

    YOUR_CUSTOM_POST_TITLE_HERE' is clear 😉

    But it seems not to work.

    #29665
    Ole PoetterOle Poetter
    Participant

    my mistake...it works like a charm.

    Thank you very much Roger!!!

    #29666
    RogerRoger
    Participant

    cool! Glad that i whas able to help someone this time 🙂

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.