Custom Fields
Re-Envisioned
Support MB Custom Post Type 2 field value’s to the title

  • Creator
    Topic
  • #19542
    Resolved Roger
    Participant

    Can anyone help me with this?
    I want to have 2 field values to be saved as a title in a CPT.

    I have a CPT called “Groepen”
    This CPT ha 2 fields, 1 is a text field and one is a time field.
    I want to have the text field first and after that a – and then the time

    [field value from text field] – [field value from time field]
    group – 15:00

    Anyone know the right php code for it? i’m stuckon this for days now.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #19547
    Long Nguyen
    Moderator

    Hi,

    You can create two fields text and time, assign to the CPT groepen.

    Then use the filter the_title to show the field value as the title of CPT. Follow my sample code

    add_filter( 'the_title', 'change_post_title_by_field_value', 99, 2 );
    
    function change_post_title_by_field_value( $title, $post_id ) {
        if( get_post_type( $post_id ) == 'groepen' ) {
            $title = rwmb_meta( 'text' ) . ' - ' . rwmb_meta( 'time' );
        }
    
        return $title;
    }

    see my screen record https://www.loom.com/share/1ecaa40b23d749f7b8315c1362bd602c
    and this documentation https://developer.wordpress.org/reference/hooks/the_title/

    #19551
    Roger
    Participant

    Really thank you!

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