I'm trying to update the post meta of all published posts hourly (via wp_cron) with the code below:

if ( ! wp_next_scheduled( 'postviews_updatedcounts_hourly_event' ) ) {
    wp_schedule_event( time(), 'hourly', 'postviews_updatedcounts_hourly_event' );

add_action( 'postviews_updatedcounts_hourly_event', 'postviews_updatedcounts_function' );


function postviews_updatedcounts_function() {

global $post;
$posts = get_posts('post_status=publish');

    foreach( $posts as $post ) {

      $post_id = $post->ID;

      $seconds = current_time( 'timestamp' ) - get_the_time('U'); // time ago in seconds that this article was published
      $minutes = round(abs($seconds/60)); // turn it into minutes

      $post_key = "postviews_hourly-refresh_" . $minutes . ""; // add post meta with time ago in minutes

      $total_post_key_views = get_post_meta( $post_id, 'jetpack-post-views', true ); // get current number of views (total)

      update_post_meta( $post_id, $post_key, $total_post_key_views ); // use new key to store number of views as of hook fire

      $updated_postviews = get_post_meta( $post_id, $post_key, true  ); // retrieve new count

      $differential = $total_post_key_views - $updated_postviews; // subtract new count from total count to determine difference, daily

      update_post_meta( $post_id, 'postviews_hourly_differential', $differential ); // add new custom field that only calculates differential



Yet, for some strange reason this will not save to the post. I've tried triggering the cron manually (with the Crontrol plugin) - still no luck. Strangely enough, the function does run with the save_post action and it does run when I load the function directly into single.php (not doing this for performance reasons).

Does anyone have any idea why? Is it something with wp_cron - something that I'm missing? Note - other wp_cron commands are executing just fine. It's something with this one in particular that's causing a problem.

  • You are trying to update post meta for all of your posts at the same time? Are you sure you aren't better off (performance) updating for a single post when the post is edited or displayed. – s_ha_dum Oct 29 '14 at 22:29
  • @s_ha_dum - yes, edited won't work, because the postviews count needs to be updated automatically hourly (or daily) across the board. The idea is to get the postviews differential every hour (or everyday) to highlight percentage increases and style accordingly. – Caroline Oct 29 '14 at 23:23
  • I suspect that you will get better performance updating on page load (via an AJAX request if possible). – s_ha_dum Oct 29 '14 at 23:26
  • @s_ha_dum ok, what action hook is that with? init? – Caroline Oct 29 '14 at 23:28
  • @s_ha_dum it still doesn't solve the need to store the updated postviews count to the post meta - the idea is to be able to caputre the difference in views between hour 1 and hour 2, then hour 2 and hour 3, etc. – Caroline Oct 29 '14 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.