Home > database >  Generate WordPress Post Tittle from ACF Fields
Generate WordPress Post Tittle from ACF Fields

Time:03-23

I am trying to generate a custom post title from two ACF fields. I have first_name and last_name but it will only generate the last name and does it twice:

/contacts/deere-deere/

Here's my function:

function update_contacts_title( $value, $post_id, $field ) {
    
    $first_name = get_field('first_name', $post_id). ' ' . $value;
    $last_name = get_field('last_name', $post_id). ' ' . $value;
    $title = $first_name .' - '. $last_name;

    $slug = sanitize_title( $title );

    $postdata = array(
        'ID'          => $post_id,
        'post_title'  => $title,
        'post_type'   => 'contacts',
        'post_name'   => $slug
);

    wp_update_post( $postdata );
    return $value;
    
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);

If I modify this line down like so, then it will generate the first_name but only once: /contacts/john/

//Create new title based on ACF Fields
function update_contacts_title( $value, $post_id, $field ) {
    
    $first_name = get_field('first_name', $post_id). ' ' . $value;
    $last_name = get_field('last_name', $post_id). ' ' . $value;
    //$title = $first_name .' - '. $last_name;
    $title = $first_name;

    $slug = sanitize_title( $title );

    $postdata = array(
        'ID'          => $post_id,
        'post_title'  => $title,
        'post_type'   => 'contacts',
        'post_name'   => $slug
);

    wp_update_post( $postdata );
    return $value;
    
}
add_filter('acf/update_value/name=first_name', 'update_contacts_title', 10, 3);
//add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);

This is really confusing me as to why it won't recognize both the first name and last name.

Any suggestions would be extremely helpful, thank you all.

CodePudding user response:

You have "first_name" for both the names I believe.

$first_name = get_field('first_name', $post_id). ' ' . $value;
$last_name = get_field('first_name', $post_id). ' ' . $value;

CodePudding user response:

Here's the answer I figured out after I remembered that I have the first_name and last_name in a group, which requires a repeater field in the function:

function update_contacts_title($post_id) {

if( have_rows('contact_info') ):
    while( have_rows('contact_info') ): the_row(); 

        $first_name = get_sub_field('first_name', $post_id);
        $last_name = get_sub_field('last_name', $post_id);
        $title = $first_name . '-' . $last_name;
        $slug = sanitize_title( $title );

    endwhile;
endif;

    $postdata = array(
        'ID'          => $post_id,
        'post_title'  => $title,
        'post_type'   => 'contacts',
        'post_name'   => $slug
    );

    wp_update_post( $postdata );
    

}
add_filter('acf/save_post', 'update_contacts_title', 10, 3);

  • Related