admin管理员组文章数量:1130349
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)
1 Answer
Reset to default 0Now 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 );
}
}
}
}
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)
-
What sort of debugging have you done? Have you verified that
$post_idsgets populated? Have you verified the values for$expire_dateand$todays_dateare correct? Have you verified that it passes theifcondition andwp_set_post_termsis called? Have you looked at whatwp_set_post_termsreturns? – 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
fieldshould beterm_idin a tax query, notid. – Milo Commented Dec 1, 2018 at 17:56 -
Thank you,
$post_idsis 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_poststonew WP_Query, then you can inspect$post_ids->requestafter the query is run and you'll see the SQL being sent to the database. – Milo Commented Dec 1, 2018 at 18:33
1 Answer
Reset to default 0Now 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 );
}
}
}
}
本文标签: custom post typeswpsetpostterms not updating with WP Cron Event
版权声明:本文标题:custom post types - wp_set_post_terms not updating with WP Cron Event 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749135800a2321346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$post_idsgets populated? Have you verified the values for$expire_dateand$todays_dateare correct? Have you verified that it passes theifcondition andwp_set_post_termsis called? Have you looked at whatwp_set_post_termsreturns? – Milo Commented Dec 1, 2018 at 17:46fieldshould beterm_idin a tax query, notid. – Milo Commented Dec 1, 2018 at 17:56$post_idsis still empty though I'm afraid - no errors or notices being thrown up – richerimage Commented Dec 1, 2018 at 18:17get_poststonew WP_Query, then you can inspect$post_ids->requestafter the query is run and you'll see the SQL being sent to the database. – Milo Commented Dec 1, 2018 at 18:33