I'm using Advanced Custom Field's acf_form function to expose a front-end form for users to post content on the website. Users will be submitting photos of redfish they have caught, and one of the datapoints is spot_count (integer).

When a new post gets published, I want to update the author's usermeta with the following:

  1. total spots
  2. most spots, single fish

To get this information, I've been using wp_query to iterate over their submissions and keep a running total, plus locate the highest spot count. This part works fine on a normal page.

But for some reason, when I hook into the save_post action in WordPress, running WP_Query doesn't include the data from the post that triggered the action. This is resulting in incorrect counts for total spots and most spots.

Is there a different hook I'm supposed to use for this? Or will this just not work because the data doesn't exist yet for WP_Query to find it?

Here's my current code:

 * Update Angler Profile whenever they post a catch (or we manually edit one)

function update_angler_profile( $post_id, $post, $update ) {

    // Query the author's redfish madness posts
    $t1stats_args = array(
    'post_type'      => 't1',
    'posts_per_page' => -1,    
    'author' => $post->post_author

    // Initialize variables
    $total_spots = 0;
    $most_spots = 0;

    // Loop through posts
    $t1stats_query = new WP_Query($t1stats_args);

                // Add current spot_count to overall counter
                $total_spots += get_field('spot_count');

                // If this post's spot_count is higher than the previous, set it as the new winner
                if (get_field('spot_count') > $most_spots){
                    $most_spots = get_field('spot_count');


    // Save total spots and most spots to the user's metadata
    update_user_meta($post->post_author, '_t1_most_spots', $most_spots);
    update_user_meta($post->post_author, '_t1_total_spots', $total_spots);


// Hook into save_post for our t1 custom post type
add_action( 'save_post_t1', 'update_angler_profile', 20, 3 );

The trick is to call the hook "wp_insert_post". But the post-type is not what you expect - post, page oder custom post type. It's "revision".

Now you can fire a WP_Query right after the post is saved in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.