0

If I have something like the following in my single.php file (as a very basic example):

<?php
$a = the_field('test1'); ?>
$b = the_field('test2'); ?>
$c = the_field('test3'); ?>
$d = the_field('test4'); ?>

$New_data = $a * $b * $c * $d;
?>

How can I use Cron to calculate $New_data on a daily basis instead of on every page refresh? In all the cron tutorials I've read, they say to use wp_schedule_event in the functions.php file, but I'm not sure how to apply that as the calculations are based on the post being viewed and not a global calculation.

Any suggestions will be greatly appreciated, been stuck on for hours!

0

It sounds like what you really might want is a transient. WP Transients are a way to cache the results of a query, and set an expiration date on that cache.

So you could cache the results of your example from above for 24 hours. If anyone requests that data while the cache is still valid, it just returns the results without recalculating.

If someone requests the data after your cache period has expired, you do the calculation again and cache the result for another 24 hours.

Example:

if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {

    // It wasn't there, so regenerate the data and save the transient

    $a = the_field('test1');
    $b = the_field('test2');
    $c = the_field('test3');
    $d = the_field('test4');

    $special_query_results = $a * $b * $c * $d;

    set_transient( 'special_query_results', $special_query_results, 24 * HOUR_IN_SECONDS );
}

// Use $special_query_results as needed from here

More info about WordPress Transients

UPDATE

Based on your further information, you may actually need a CRON, here is an example:

if ( ! wp_next_scheduled( 'update_acf' ) ) {
    wp_schedule_event( time(), 'daily', 'update_acf' );
}

add_action( 'update_acf', 'update_my_acf_field' );

function update_my_acf_field() {
    // This is where you will do the logic that needs to be performed on a daily CRON
}
  • Thank you, that definitely sounds like it might work. One question though, if the calculation is multiple lines, such as ob_start(); echo floor($total); $OverallScore = ob_get_contents(); ob_end_clean(); update_field( 'score', $OverallScore );, how would I insert that into the $special_query_results line? – Guit4eva Nov 8 '14 at 16:46
  • That code is pretty different than the code in your question, because it is updating a value in the database which would be used elsewhere in a template/script. In that case you would need a transient. I updated my answer to include an example of a WP CRON. – Matt Keys Nov 8 '14 at 16:53
  • It is also worth mentioning that the use of output buffering in your code is worth taking a second look at. Code like that is very rarely the 'proper' way to accomplish your goals in WordPress. I cannot tell from that code what problem the output buffering is trying to solve so I cannot make any specific recommendations. – Matt Keys Nov 8 '14 at 16:56
  • Hi Matt, thanks for your answers, though I'm still a bit in the dark:/ Does that updated snippet go in the functions.php file? And if so, how do I get it to work for multiple posts? If I put it straight into the page.php file, I can see the cron set up through Crontrol but the function doesn't fire when it's supposed to (even if I press "run now") – Guit4eva Nov 8 '14 at 17:57
  • Yes that would go either into a custom plugin, or the functions.php – Matt Keys Nov 8 '14 at 17:58

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.