I have a custom php loop/function written that works in the following way

$start = strtotime(date("Y-m-d H:i:s", strtotime("+5 hours")));
$end = get_post_meta( $post->ID, '_limited_dates_to', true );
$remaining = $end - $start; 
if ($end > $start) {
    wp_set_post_terms( $post->ID, 'not active', 'product_tag', false );
} else {
    wp_set_post_terms( $post->ID, 'active', 'product_tag', false );

Currently the loop works fine but it is attached with a timer as one can see easily in $start and $end. Now what I am trying to get here is something of a product expiry thing going on. I need the cron to work this if & else loop and automatically set the active or not active tags.

How can this be achieved?
Currently I have to manually refresh the page so that the loop runs.

  • Where is this code? What triggers it right now?
    – s_ha_dum
    Jun 11, 2013 at 4:07
  • I have this code in the custom post type template. I am using WooCommerce. It is the single-product.php file [link]pastebin.ca/2395360[/link] Jun 11, 2013 at 4:12
  • any updates on this? Jun 24, 2013 at 10:21
  • I don't see that code in the pastebin. What line numbers is it on? Also, you've hacked WooCommerce?
    – s_ha_dum
    Jun 24, 2013 at 13:46
  • Yes woocommerce has been hacked link Jul 8, 2013 at 7:40

1 Answer 1


wp_cron is a PHP level system and so also only runs on page load. It does not run on clock time so you'd still need to reload the page to get the loop to run.

Second, to get this working with wp_cron you'd need to pull all of your posts with _limited_dates_to set and loop through them. You'd need to do that every time the wp_cron job runs. That is likely to be a lot of server load as I doubt your expiration times fall into neat blocks that allow you to run a wp_cron job every few hours.

As is, you can check one post at a time as needed. That, I would argue, is the better way of doing this than using wp_cron.

Even if you have server access that allows you to set up a real system cron job you still have the same problem with needing to Loop through all posts with _limited_dates_to set.

Unless you have a very convincing reason that this needs to use wp_cron or cron, and it works as is, I'd say "leave it alone". I don't see the benefits and I do see potential drawbacks.

  • I would agree.... and specially running such a thing on a VPS would create a lot of havoc. Jul 15, 2013 at 6:01

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.