$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom post types - wp_set_post_terms not updating with WP Cron Event|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - wp_set_post_terms not updating with WP Cron Event

matteradmin9PV0评论

I have a jobs board custom post type (job_listings) with a taxonomy (cvl_job_status) attached with various tags assigned, namely; Live, Filled and Expired.

Each job post has a custom field (cvl_job_expires) with an expiry date.

Using a WP Cron Event I want to change the taxonomy tag from Live to Expired if today's date is greater than the saved expiry date.

Can't see what's wrong with the following code, first and foremost $post_ids is returning an empty array.

can anyone help?

TIA

add_action('cvl_job_status_cron', 'cvl_mark_as_expired');

function cvl_mark_as_expired() {

  global $post;

  $post_ids = get_posts( 
    [
      'post_type' => 'job_listing',
      'posts_per_page' => -1,
      'no_found_rows' => true,
      'fields' => 'ids', //again, for performance
      'tax_query' => array(
        array(
          'taxonomy' => 'cvl_job_status',
          'field' => 'id',
          'terms' => 158, // ID of 'Live' tag
        )
      )
    ] 
  );

  var_dump($post_ids); // this returns as empty??

  foreach($post_ids as $post_id) {

    $key = 'cvl_job_expires'; // custom field name
    $expire_date = get_post_meta($post_id, $key, true); // Expiry Date saved as (d M y)
    $todays_date = date('d M y'); // get todays date

    if ($expire_date < $todays_date) {

      $taxonomy = 'cvl_job_status';
      $tag      = array( 159 );  // ID of 'Expired' tag

      wp_set_post_terms( $post_id, $tag, $taxonomy );

      //  I have also tried this with
      //  wp_set_object_terms( $post_id, $tag, $taxonomy );

    }

  }

}

(The cvl_job_status_cron event is running and this function cvl_mark_as_expired is attached to it as shown in the Wp Cron-Events Plug-In)

I have a jobs board custom post type (job_listings) with a taxonomy (cvl_job_status) attached with various tags assigned, namely; Live, Filled and Expired.

Each job post has a custom field (cvl_job_expires) with an expiry date.

Using a WP Cron Event I want to change the taxonomy tag from Live to Expired if today's date is greater than the saved expiry date.

Can't see what's wrong with the following code, first and foremost $post_ids is returning an empty array.

can anyone help?

TIA

add_action('cvl_job_status_cron', 'cvl_mark_as_expired');

function cvl_mark_as_expired() {

  global $post;

  $post_ids = get_posts( 
    [
      'post_type' => 'job_listing',
      'posts_per_page' => -1,
      'no_found_rows' => true,
      'fields' => 'ids', //again, for performance
      'tax_query' => array(
        array(
          'taxonomy' => 'cvl_job_status',
          'field' => 'id',
          'terms' => 158, // ID of 'Live' tag
        )
      )
    ] 
  );

  var_dump($post_ids); // this returns as empty??

  foreach($post_ids as $post_id) {

    $key = 'cvl_job_expires'; // custom field name
    $expire_date = get_post_meta($post_id, $key, true); // Expiry Date saved as (d M y)
    $todays_date = date('d M y'); // get todays date

    if ($expire_date < $todays_date) {

      $taxonomy = 'cvl_job_status';
      $tag      = array( 159 );  // ID of 'Expired' tag

      wp_set_post_terms( $post_id, $tag, $taxonomy );

      //  I have also tried this with
      //  wp_set_object_terms( $post_id, $tag, $taxonomy );

    }

  }

}

(The cvl_job_status_cron event is running and this function cvl_mark_as_expired is attached to it as shown in the Wp Cron-Events Plug-In)

Share Improve this question edited Dec 1, 2018 at 18:52 richerimage asked Dec 1, 2018 at 17:30 richerimagericherimage 1014 silver badges12 bronze badges 8
  • What sort of debugging have you done? Have you verified that $post_ids gets populated? Have you verified the values for $expire_date and $todays_date are correct? Have you verified that it passes the if condition and wp_set_post_terms is called? Have you looked at what wp_set_post_terms returns? – Milo Commented Dec 1, 2018 at 17:46
  • Sorry - possibly the most important thing I forgot to add - $post_ids is returning an empty array() !! – richerimage Commented Dec 1, 2018 at 17:51
  • 1 field should be term_id in a tax query, not id. – Milo Commented Dec 1, 2018 at 17:56
  • Thank you, $post_ids is still empty though I'm afraid - no errors or notices being thrown up – richerimage Commented Dec 1, 2018 at 18:17
  • 1 Try changing get_posts to new WP_Query, then you can inspect $post_ids->request after the query is run and you'll see the SQL being sent to the database. – Milo Commented Dec 1, 2018 at 18:33
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Now Working with Wp Query - Thanks to Milo in the comments

add_action('cvl_job_status_cron', 'cvl_mark_as_expired');

function cvl_mark_as_expired() {

  global $post;

  $args = array(
    'post_type'              => array( 'job_listing' ),
    'posts_per_page'         => '-1',
    'fields'                 => 'ids',
    'tax_query'              => array(
      'relation' => 'AND',
      array(
        'taxonomy'         => 'cvl_job_status',
        'terms'            => 'live',
        'field'            => 'slug',
        'include_children' => false,
      ),
    ),
  );

// $post_ids = get_posts($args);

  $post_ids = new WP_Query( $args );

  if ( $post_ids->have_posts() ) {

    while ( $post_ids->have_posts() ) {

    $post_ids->the_post();

    $post_id = get_the_ID();

    $key          = 'cvl_job_expires'; // custom field anme
    $expire_date  = get_post_meta($post_id, $key, true); // Now saved as Unix Timestamp
    $todays_date  = time(); // get todays date

      if ($expire_date < $todays_date) {

        $taxonomy = 'cvl_job_status';
        $tag      = array( 159 );  // 'Expire' tag id is 159

        wp_set_post_terms( $post_id, $tag, $taxonomy );

      }

    }

  }

}
Post a comment

comment list (0)

  1. No comments so far