1

I have a custom post type ("fecha") that contains past events (birthdates of famous people, historical dates, etc). Within these posts, a custom field ("aniversario") calculates how many years have passed since the original date.

This value is stored when the post is created and saved, so I need to update these custom posts (all of them) every year so that this custom field keeps being valid. I am trying to use cron (with a daily schedule) to call an update_post_meta, without success. All posts in these custom post type are private and appear only in the admin side.

The website is personal and for non-comercial pourposes (I´m a freelance science writer, this is an attempt to build a personal editorial calendar, wich would help me enormously).
Any help pointing errors in code or something missing, would be great. The code is as follows (it is located in my functions.php):

function run_update_fecha_cron_job() {

    $posts = get_posts([
        'post_type' => 'fecha',
        'post_status' => 'any',
        'posts_per_page' => -1, // getting all posts of a post type
        'no_found_rows' => true, // speeds up a query significantly and can be set to 'true' if we don't use pagination
        'fields' => 'ids' // again, for performance
    ]);

    // now check meta and update custom fields for every post
    foreach ($posts as $post_id) {

        /*
         * calculate how many years have passed by sustracting the value from
         * another custom field called "wpcf-ano" to current year, then store calculated value in
         * variable "$aniversario", wich should be "1" if nothing is stored in "wpcf-ano"
         */
        $ano = get_post_meta($post_id, 'wpcf-ano', TRUE);
        if (!empty($ano)) {
            $year_diff = date("Y") - $ano;
            $aniversario = $year_diff;
        } else {
            $aniversario = 1;
        }

        // update custom field
        update_post_meta($post_id, 'aniversario', $aniversario);
    }

}

// Schedule Cron Job Event
function update_fecha_cron_job() {

    if (!wp_next_scheduled('run_update_fecha_cron_job')) {
        wp_schedule_event(current_time('timestamp'), 'daily', 'run_update_fecha_cron_job');
    }

}
add_action('wp', 'update_fecha_cron_job');
add_action('run_update_fecha_cron_job', 'run_update_fecha_cron_job');
9
  • to avoid this treatment, it's better to store the birthday date on the meta and calculate the years on display.
    – mmm
    Commented May 13, 2018 at 12:00
  • I think that´s exactly what I am currently doing. Birthdate year is saved in a custom field ("wpcf-ano") and used to calculate the value of another custom field ("aniversario", depicting the number of years that have passed since then). Not sure what you are trying to say.
    – Jose
    Commented May 13, 2018 at 12:03
  • for "wpcf-ano", I think it's ok. but for "aniversario" it would be easier to calculate this value only when you want to display the value, that should not use to much resources.
    – mmm
    Commented May 13, 2018 at 13:16
  • I want to show and use that value ("aniversario") as a column and filter in the admin post list for this custom post type ("fecha"). For that reason, I need it to be stored as a custom field. Could you detect something wrong in the code I posted? Thanks.
    – Jose
    Commented May 13, 2018 at 13:44
  • verify that "get_post" return what you need. by default only published posts are returned.
    – mmm
    Commented May 13, 2018 at 16:34

1 Answer 1

1

the third argument of wp_schedule_event is a action to call :
https://codex.wordpress.org/Function_Reference/wp_schedule_event

then you need to hook the fonction at the action like this :

add_action("run_update_fecha_cron_job", "run_update_fecha_cron_job");
3
  • Now it works! Many thanks! I add it to the code above so to have everything together.
    – Jose
    Commented May 14, 2018 at 14:23
  • in the code above you removed the code for the hook wp but you need the two of them. on the action wp you define the cron if it doesn't exist and and on the action run_update_fecha_cron_job you run the treatment.
    – mmm
    Commented May 14, 2018 at 17:09
  • Is it now correct?
    – Jose
    Commented May 14, 2018 at 20:32

Your Answer

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

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