Custom Fields
Re-Envisioned
Support Meta Box Builder map the value of custom field into wp default post_titel

  • Creator
    Topic
  • #29656
    Resolved Ole 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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #29661
    Roger
    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 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
    Roger
    Participant

    what is the name from your custom post type?

    #29664
    Ole Poetter
    Participant

    I edited my reply....

    YOUR_CUSTOM_POST_TITLE_HERE' is clear 😉

    But it seems not to work.

    #29665
    Ole Poetter
    Participant

    my mistake...it works like a charm.

    Thank you very much Roger!!!

    #29666
    Roger
    Participant

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

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