Rob
Rob

Reputation: 1636

ACF custom field. Get field inside action within functions.php

Wordpress + Timber + ACF Pro. Inside functions.php, I have an action that is triggered whenever a post (with a type of week) is published.

I would like to take data from this post, and use it to create a new post for every single user.

I have it working after a fashion. When a post is published, I grab the title and the username and use this as the title for newly created post.

However, I hit problems when trying to pull in ACF data - eg: a week_commencing date field. All ACF data returns NULL (I know the fields are populated).

I have read the docs - which state to access data, you need to call get_field('field_name', 'post_id') - which I have done.

I've written out the $ID - so know this is correct.

Could it be due to the order in which I run things?

Here is my code:

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

** EDIT **

It turns out that the wordpress post was being saved before the ACF fields were being created? So a pal refactored my code to use a different event. However, this isn't triggered when the post is published...

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

Upvotes: 0

Views: 3310

Answers (1)

Rob
Rob

Reputation: 1636

So now testing post status for when published - and doing what's needed then. Triggering action if old_status == future, and new_status == publish seems to do the trick.

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

Upvotes: 0

Related Questions