0

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)

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? Commented Dec 1, 2018 at 17:46
  • Sorry - possibly the most important thing I forgot to add - $post_ids is returning an empty array() !! Commented Dec 1, 2018 at 17:51
  • 1
    field should be term_id in a tax query, not id. 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 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. Commented Dec 1, 2018 at 18:33

1 Answer 1

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 ); } } } } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.