0

I'm using ACF in my custom post type and I have 2 numeric fields: one for Birth year and another for Age. I want to update the age field automatically based on the birth year field value. I found out an acf filter that updates the field value before it is saved into the database and created the following function:

function update_acf_value_age( $value, $post_id, $field, $original ) {
    $birth_year = get_field( 'birth_year', $post_id );
    $value = date('Y') - $birth_year;

    return $value;
}
add_filter('acf/update_value/name=age', 'update_acf_value_age', 10, 4);

It is working fine and updates the age value every time I save a certain post. But I need it to be updated every year, ideally automatically. Is there a way to automatically force every post to be saved/updated yearly? Or is there a better approach to what I'm trying to accomplish?

I read about something called WP-Cron but I don't understand how to implement it and don't know if it's a good solution for this case.

Thanks in advance!

2
  • 1
    What is your use for the age field? Obviously age can be roughly calculated from birth year, so it depends on what the usage of age is.
    – Caleb
    Jul 13 at 16:25
  • @Caleb Thanks for your comment. The age field will be used to filter the custom post types of actors using the plugin Filter Everything. In this case we only have the birth year, not the birth date, so it's ok that the age value will only be roughly calculated. But the question here is if it's possible to update the value yearly without having to manually save each post every year. Jul 13 at 16:40

1 Answer 1

0

If you don't want to use a scheduler (ex: Action Scheduler) or a cron job, and you only want to update once per year, then an option that tracks the next update time is probably the simplest approach (untested):

add_action( 'init', static function () {
    $option_name = 'cpt_ages_next_update';
    $option      = absint( get_option( $option_name ) );

    if ( $option > time() ) {
        return;
    }

    $query = new WP_Query( array(
        'post_type'      => 'cpt',
        'posts_per_page' => -1,
        'fields'         => 'ids',
        'no_found_rows'  => true,
    ) );

    if ( ! $query->have_posts() ) {
        return;
    }

    foreach ( $query->posts as $post_id ) {
        $year = absint( get_field( 'birth_year', $post_id ) );
        $age  = absint( date( 'Y' ) ) - $year;

        update_field( 'age', $age );
    }

    update_option( $option_name, time() + YEAR_IN_SECONDS );
} );

I would recommend using a plugin like Action Scheduler though, especially if you the number of posts is very high.

6
  • Thanks for your answer. I didn't say I don't want to use a scheduler or a cron job, I've just said I don't know how it works or how to implement it. I'm a beginner to PHP and WP developement. In terms of number of posts, it will possibly be 500-600. I will take a look at the plugin Action Scheduler. Thanks for the help! Jul 13 at 17:21
  • I'm testing the code in your answer. I changed the "YEAR_IN_SECONDS" to "MINUTE_IN_SECONDS" just for testing. I had to add the $post_id in update_field( 'age', $age, $post_id ); to get it to work. It's updating the custom field age correctly in the post but it's not updating the respective age filters on the front-end of the website. Already tried to clean all caches but it didn't work. If I manually save any post, all ages get updated in the front-end filters (not only the saved post). Any idea of what could be missing here? I will also try to adapt this code and use Action Scheduler. Jul 14 at 16:05
  • I imagine that the Filter Everything plugin has its own cache, which gets purged on post save: the code above does not save a post.
    – Caleb
    Jul 14 at 16:07
  • Just tried it with the plugin Action Scheduler and it works in the same way. It's just not updating in front-end without saving a post. Maybe that's not a big deal, since it only needs to be updated yearly and at some point a post will be updated. Do you think it's a bit overkill to use Action Scheduler for this or its the better option given the number of posts (500-600)? Jul 14 at 17:32
  • Also I need to get it done for 2 different cpt. Should I add an array with the 2 cpt in WP_Query or, in terms of performance, is it a better idea to create separated Scheduled Actions for each cpt? Sorry for all these questions and thank you so much for all the help. Jul 14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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