Wordpres Cron Jobs - Changing post meta after set time

588 views Asked by At

I'm having trouble getting a cron job working in wordpress.

Setup: I have a custom post type ('jobs') that have a couple of meta fields: 'featured' (bool) set to true if it is featured and 'feature-expiry' (DateTime) which is the time that the feature expires.

Goal: Once the feature-expiry time has passed, update the post meta and change 'featured' to false

I'm kindof new to php and I can't seem to get it to work. The cron job fires fine but the posts aren't being updated, what am I doing wrong?

/*           jj cron jobs              */

function jj_feaurecheck_cron_function( ) {

  global $post;

  $args = array( 
    'post_type'       => 'jobs',
    'posts_per_page'  => -1,
  );

  $listings = get_posts( $args );
    foreach($listings as $post) : setup_postdata($post);

    $today = date( 'Ymd' );
    $expire = get_field( 'feature-expiry', false, false );
    $status = get_field( 'featured' );
        if ( $expire < $today ) :
            $status = 'false';
            update_field( 'featured', $status );
        endif;  
    endforeach;

}
if ( ! wp_next_scheduled( 'jj_feaurecheck_cron' ) ) {
    wp_schedule_event( date( 'Ymd' ), 'daily', 'jj_feaurecheck_cron' );
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );
2

There are 2 answers

1
Steven Jupiter On BEST ANSWER

[---- Fix ----]

get_field() and update_field() are for use with Advanced Custom Fields, which my project isn't using. swtiched to get_post_meta() and update_post_meta().

if ( $expire < $today ) for some reason was affecting posts set to expire in the future so I switched them to unix time and that did the trick.


/*           jj cron jobs              */

function jj_feaurecheck_cron_function( ) {

  global $post;

  $args = array( 
    'post_type'       => 'jobs',
    'posts_per_page'  => -1,
  );

  $listings = get_posts( $args );
    foreach($listings as $post) : setup_postdata($post);
    
    $today = date( 'Ymd' );
    $expire = get_post_meta($post->ID, 'feature-expiry', true );
    $status = get_post_meta($post->ID, 'featured' );
    
      //get current date
      $today = new DateTime();

      //convert expire into a date obj
      $expire_date = new DateTime($expire);

      //convert dates to seconds for easier comparison
      $expire_secs = $expire_date->format('U');
      $today_secs  = $today->format('U');
    
      if ( $expire_secs < $today_secs ) :
          $status = 'false';
          //featured set to false
          update_post_meta($post->ID, 'featured', $status ); 
          //feature-expiry set back to empty
          update_post_meta($post->ID, 'feature-expiry', '' );
      endif;  
    endforeach;

}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );

Thank you disinfor and Steven for your guidance, I can't tell you how much I appreciate it! =]

4
AudioBubble On

try this, I cannot test it so hopefully there are no errors but this is the general format i use to do what you are trying.

/**
 * jj cron jobs
 */
function jj_feaurecheck_cron_function( ) {

  $args = array(
    'post_type'       => 'jobs',
    'posts_per_page'  => -1,
  );

  $query = new WP_Query($args);

  if($query->have_posts()) {

    while($query->have_posts()) {

      the_post();

      //get the post id
      $post_id = get_the_ID();

      //get fields attached to post using ACF
      $expire = get_field( 'feature-expiry', $post_id, false );
      $status = get_field( 'featured', $post_id );

      //if not using ACF
      $expire = get_post_meta( $post_id, 'feature-expiry', true );
      $status = get_post_meta( $post_id, 'featured', true );

      //get current date
      $today = new DateTime();

      //convert expire into a date obj
      $expire_date = new DateTime($expire);

      //convert dates to seconds for easier comparison
      $expire_secs = $expire_date->format('U');
      $today_secs  = $today->format('U');

      //do the comparison
      if( $expire_secs < $today_secs ) {

          //update the field for that post ACF
          $status = 'false';
          update_field( 'featured', $status, $post_id );

          //non ACF
          update_post_meta($post_id, 'featured', $status);
      } 
    }
  }
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );