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

时间:2019-01-17 22:21:06

标签: php wordpress advanced-custom-fields

我想基于ACF字段自动填写三种自定义帖子类型(CPT)的标题。我在下面找到了代码,但不知道如何为三个CPT(而不是一个)编写它。我会感谢您的帮助!

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 个答案:

答案 0 :(得分:0)

尝试此代码。它应该为您工作。

<?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 );

更新

用以下内容替换之前的代码。它将解决以下评论中由Andrew指出的双重标题问题。

<?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 );

经过测试并正在研究:

  1. WordPress 5.0.3
  2. 二十一岁少年1.2
  3. 高级自定义字段PRO 5.7.10
  4. Localhost(适用于Windows 5.6.15的XAMPP)