donglefu6195
2019-01-17 22:21
浏览 109
已采纳

自动从ACF字段填写自定义帖子类型标题

I'd like to auto fill the title of three Custom Post Types (CPTs) based on an ACF field. I found the code below, but don't know how to write it for three CPTs instead of just one. I would appreciate the help!

function acf_title( $value, $post_id, $field ) {
    if ( get_post_type( $post_id ) == 'companies' ) {

        $new_title = get_field('company_name', $post_id) . ' ' . $value;
        $new_slug = sanitize_title( $new_title );

        wp_update_post( array(
            'ID'            => $post_id,
            'post_title'    => $new_title,
            'post_name'     => $new_slug,
            )
        );
    }
    return $value;
} 

add_filter('acf/update_value', 'acf_title', 10, 3);
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dongxuan2015 2019-01-24 03:40
    最佳回答

    Try this code. It should work for you.

    <?php
    // Auto fill Title and Slug for 'companies' CPT
    function acf_title_companies( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'companies' ) {
            $new_title = get_field( 'company_name', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=company_name', 'acf_title_companies', 10, 3 );
    // Auto fill Title and Slug for 'contacts' CPT
    function acf_title_contacts( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'contacts') {
            $new_title = get_field( 'name_first', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=name_first', 'acf_title_contacts', 10, 3 );
    // Auto fill Title and Slug for 'properties' CPT
    function acf_title_properties( $value, $post_id, $field ) {
        if ( get_post_type( $post_id ) == 'properties') {
            $new_title = get_field( 'building_name', $post_id ) . ' ' . $value;
            $new_slug = sanitize_title( $new_title );
            wp_update_post( array(
                'ID'         => $post_id,
                'post_title' => $new_title,
                'post_name'  => $new_slug,
                )
            );
        }
        return $value;
    } 
    add_filter( 'acf/update_value/name=building_name', 'acf_title_properties', 10, 3 );
    

    Update

    Replace the previous code with the following. It will resolve the double title issue as noted by Andrew in below comment.

    <?php
    
    // Auto fill Title and Slug for CPT's - 'companies', 'contacts', 'properties'
    function lh_acf_save_post( $post_id ) {
    
        // Don't do this on the ACF post type
        if ( get_post_type( $post_id ) == 'acf' ) {
            return;
        }
    
        $new_title = '';
        $new_slug = '';
    
        // Get title from 'companies' CPT acf field 'company_name'
        if ( get_post_type( $post_id ) == 'companies' ) {
            $new_title = get_field( 'company_name', $post_id );
            $new_slug = sanitize_title( $new_title );
        }
    
        // Get title from 'contacts'CPT acf field 'contact_name'
        if ( get_post_type( $post_id ) == 'contacts') {
            $contact_name = get_field( "contact_name" );
            if ( $contact_name ) {
                $name_first = $contact_name['name_first'];
                $name_last = $contact_name['name_last'];
            }
            $new_title = $name_first . ' ' . $name_last;
            $new_slug = sanitize_title( $new_title );
        }
    
        // Get title from 'properties' CPT acf field 'building_name'
        if ( get_post_type( $post_id ) == 'properties') {
            $new_title = get_field( 'building_name', $post_id );
            $new_slug = sanitize_title( $new_title );
        }
    
        // Prevent iInfinite looping...
        remove_action( 'acf/save_post', 'lh_acf_save_post' );
    
        // Grab post data
        $post = array(
            'ID'            => $post_id,
            'post_title'    => $new_title,
            'post_name'     => $new_slug,
        );
    
        // Update the Post
        wp_update_post( $post );
    
        // Continue save action
        add_action( 'acf/save_post', 'lh_save_post' );
    
        // Set the return URL in case of 'new' post
        $_POST['return'] = add_query_arg( 'updated', 'true', get_permalink( $post_id ) );
    }
    add_action( 'acf/save_post', 'lh_acf_save_post', 10, 1 );
    

    Tested and working on:

    1. WordPress 5.0.3
    2. Twentyninteen 1.2
    3. Advanced Custom Fields PRO 5.7.10
    4. Localhost (XAMPP for Windows 5.6.15)
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题