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)
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_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? – Milo Commented Dec 1, 2018 at 17:46field
should beterm_id
in a tax query, notid
. – Milo Commented Dec 1, 2018 at 17:56$post_ids
is still empty though I'm afraid - no errors or notices being thrown up – richerimage Commented Dec 1, 2018 at 18:17get_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. – Milo Commented Dec 1, 2018 at 18:33