最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Update posts after populating ACF field value

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I'm running a function to populate some custom fields with some values specified:

add_filter('acf/load_field/name=event_end_time', function($endField) {
    $endDate = get_field('event_end_date');
    $endHour = get_field('event_end_hour');
    $endMinute = get_field('event_end_minute');
    $endMeridian = get_field('event_end_meridian');
    $end12 = $endHour . ':' . $endMinute . $endMeridian;
    $end24 = date("H:i:s", strtotime($end12));
    $finalEndDateTime = $endDate . ' ' . $end24;
    $endField['value'] = __($finalEndDateTime, 'txtdomain');
    return $endField;   
});

This populates the field event_end_time as expected and the desired values appear in the field as expected...which is great. But none of these values appear on the front-end until I go in and manually 'Update' each post.

What I'm trying to find out, but not succeeding, is whether it's possible to expand this function to 'update' the posts too so I don't have to go in and hit update myself on 500-600 posts?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this question

I'm running a function to populate some custom fields with some values specified:

add_filter('acf/load_field/name=event_end_time', function($endField) {
    $endDate = get_field('event_end_date');
    $endHour = get_field('event_end_hour');
    $endMinute = get_field('event_end_minute');
    $endMeridian = get_field('event_end_meridian');
    $end12 = $endHour . ':' . $endMinute . $endMeridian;
    $end24 = date("H:i:s", strtotime($end12));
    $finalEndDateTime = $endDate . ' ' . $end24;
    $endField['value'] = __($finalEndDateTime, 'txtdomain');
    return $endField;   
});

This populates the field event_end_time as expected and the desired values appear in the field as expected...which is great. But none of these values appear on the front-end until I go in and manually 'Update' each post.

What I'm trying to find out, but not succeeding, is whether it's possible to expand this function to 'update' the posts too so I don't have to go in and hit update myself on 500-600 posts?

Share Improve this question asked Mar 8, 2021 at 16:41 user1374796user1374796 39511 gold badges18 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

This is the normal behavior.

ACF field value will not be added directly in your post until you save it. I've been facing this "issue" lately.

this is because you have created your field after your posts. So, your posts don't have the associated value.

load_field doesn't add values on existing post, it only add your value on the post form edition.

What I suggest, is that you can do a one-shot function that update all your posts in a second.

foreach(get_posts($args) as $post) {
  $data[] = ... // your event data
  update_field('your_field_group', $data, $post->ID); 
}

be careful here, you might want to use add_row function instead of the update_field (depending on your needed) https://www.advancedcustomfields/resources/add_row/

发布评论

评论列表(0)

  1. 暂无评论