我正在使用高级自定义字段acf_form
用于向用户公开前端表单以在网站上发布内容。用户将提交他们捕获的红鱼的照片,其中一个数据点是spot\\u count(整数)。
发布新帖子时,我想用以下内容更新作者的usermeta:
为了获得这些信息,我一直在使用wp\\u query来迭代他们提交的内容,并保持一个连续的总数,再加上定位最高的点计数。这部分在普通页面上运行良好。
但出于某种原因,当我save_post
在WordPress中,运行WP\\u Query不包括触发该操作的帖子中的数据。这会导致总斑点和大多数斑点的计数不正确。
我应该用另一个钩子吗?或者,由于数据尚不存在,WP\\U查询无法找到它,这会不会行不通?
这是我当前的代码:
/**
* 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);
if($t1stats_query->have_posts()):
while($t1stats_query->have_posts()):$t1stats_query->the_post();
// 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\');
}
endwhile;
endif;
wp_reset_postdata();
// 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 );