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)
$post_ids
gets populated? Have you verified the values for$expire_date
and$todays_date
are correct? Have you verified that it passes theif
condition andwp_set_post_terms
is called? Have you looked at whatwp_set_post_terms
returns?field
should beterm_id
in a tax query, notid
.$post_ids
is still empty though I'm afraid - no errors or notices being thrown upget_posts
tonew 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.