0

WordPress + 木材 + ACF 专业版。在functions.php 中,我有一个动作,每当发布帖子(类型为星期)时都会触发该动作。

我想从这篇文章中获取数据,并使用它为每个用户创建一个新帖子。

我让它在时尚之后工作。发布帖子后,我会获取标题和用户名,并将其用作新创建帖子的标题。

但是,在尝试提取 ACF 数据时遇到问题 - 例如:week_commencing 日期字段。所有 ACF 数据都返回 NULL(我知道这些字段已填充)。

我已经阅读了文档 - 访问数据的状态,您需要调用 get_field('field_name', 'post_id') - 我已经完成了。

我已经写出了 $ID - 所以知道这是正确的。

可能是由于我运行事物的顺序吗?

这是我的代码:

function weekly_published_post_setup($ID, $post) {
    $customers = get_users();
    $theDate = get_field("week_commencing", $ID);

// Array of WP_User objects.
foreach ( $customers as $user ) {

        $new_post = array(
              'post_type' => 'weekly_tasks',
                'post_title' => $post->post_title . ' - ' . $theDate . ' - ' . $user->display_name,
                'post_content' => $theDate,
                'post_status' => 'publish',
                'post_author' => $user->ID
        );
    wp_insert_post($new_post);

        }
}
add_action( 'publish_week', 'weekly_published_post_setup',  10, 2 );

** 编辑 **

原来是在创建 ACF 字段之前保存了 wordpress 帖子?所以一个朋友重构了我的代码以使用不同的事件。但是,发布帖子时不会触发此操作...

function week_published_delivery_setup($ID) {

    $post = get_post($ID);

    if ($post->post_type != 'week') {
        return;
    }

    if( $post->post_modified_gmt != $post->post_date_gmt ){
        return;
    }

    $customers = get_users();

     $field = get_field('week_commencing', $ID);

     $fields = post.get_field_objects($ID);

     if( $fields )
     {
        foreach( $fields as $field_name => $field )
        {

                $tmp .= $field['label'] . $field['value'];
        }
    }*/



// Array of WP_User objects.
foreach ( $customers as $user ) {
        $new_delivery_post = array(
              'post_type' => 'delivery',
                'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name,
                'post_content' =>  $post->post_title,
                'post_status' => 'publish',
                'post_author' => $user->ID
        );
    wp_insert_post($new_delivery_post);


        }
}
add_action( 'acf/save_post', 'week_published_delivery_setup',  20);
4

1 回答 1

0

所以现在测试发布时的帖子状态 - 然后做需要做的事情。如果 old_status == future 和 new_status == publish 触发动作似乎可以解决问题。

function on_all_status_transitions( $new_status, $old_status, $post ) {
    $ID = $post->ID;
    if ($post->post_type != 'week') {
        return;
    }

    if ( $new_status != $old_status && $old_status == 'future' && $new_status == 'publish' ) {

            $customers = get_users();

            $field = get_field('week_commencing', $ID);



            // Array of WP_User objects.
            foreach ( $customers as $user ) {
                $new_delivery_post = array(
                        'post_type' => 'delivery',
                        'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name,
                        'post_content' =>  $post->post_title,
                        'post_status' => 'publish',
                        'post_author' => $user->ID
                );
                wp_insert_post($new_delivery_post);


                }

    }
}
add_action(  'transition_post_status',  'on_all_status_transitions', 20, 3 );
于 2017-01-23T11:45:28.773 回答