我無法在 wordpress 中獲得一份 cron 作業。
設定:我有一個自定義帖子型別('jobs'),它有幾個元欄位:'featured'(bool)設定為 true,如果它是精選的,'feature-expiry'(DateTime)是該功能的時間過期。
目標:一旦功能到期時間過去,更新帖子元并將'featured'更改為false
我對php有點陌生,我似乎無法讓它作業。cron 作業很好,但帖子沒有更新,我做錯了什么?
/* jj cron jobs */
function jj_feaurecheck_cron_function( ) {
global $post;
$args = array(
'post_type' => 'jobs',
'posts_per_page' => -1,
);
$listings = get_posts( $args );
foreach($listings as $post) : setup_postdata($post);
$today = date( 'Ymd' );
$expire = get_field( 'feature-expiry', false, false );
$status = get_field( 'featured' );
if ( $expire < $today ) :
$status = 'false';
update_field( 'featured', $status );
endif;
endforeach;
}
if ( ! wp_next_scheduled( 'jj_feaurecheck_cron' ) ) {
wp_schedule_event( date( 'Ymd' ), 'daily', 'jj_feaurecheck_cron' );
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );
uj5u.com熱心網友回復:
[ - - 使固定 - - ]
get_field()并update_field()與我的專案未使用的高級自定義欄位一起使用。切換到get_post_meta()和update_post_meta()。
if ( $expire < $today )出于某種原因,影響了將來到期的帖子,所以我將它們切換到了 unix 時間,這樣就成功了。
/* jj cron jobs */
function jj_feaurecheck_cron_function( ) {
global $post;
$args = array(
'post_type' => 'jobs',
'posts_per_page' => -1,
);
$listings = get_posts( $args );
foreach($listings as $post) : setup_postdata($post);
$today = date( 'Ymd' );
$expire = get_post_meta($post->ID, 'feature-expiry', true );
$status = get_post_meta($post->ID, 'featured' );
//get current date
$today = new DateTime();
//convert expire into a date obj
$expire_date = new DateTime($expire);
//convert dates to seconds for easier comparison
$expire_secs = $expire_date->format('U');
$today_secs = $today->format('U');
if ( $expire_secs < $today_secs ) :
$status = 'false';
//featured set to false
update_post_meta($post->ID, 'featured', $status );
//feature-expiry set back to empty
update_post_meta($post->ID, 'feature-expiry', '' );
endif;
endforeach;
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );
感謝 disinfor 和 Steven 的指導,我無法告訴你我有多感激!=]
uj5u.com熱心網友回復:
試試這個,我無法測驗它,所以希望沒有錯誤,但這是我用來做你正在嘗試的事情的一般格式。
/**
* jj cron jobs
*/
function jj_feaurecheck_cron_function( ) {
$args = array(
'post_type' => 'jobs',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
the_post();
//get the post id
$post_id = get_the_ID();
//get fields attached to post using ACF
$expire = get_field( 'feature-expiry', $post_id, false );
$status = get_field( 'featured', $post_id );
//if not using ACF
$expire = get_post_meta( $post_id, 'feature-expiry', true );
$status = get_post_meta( $post_id, 'featured', true );
//get current date
$today = new DateTime();
//convert expire into a date obj
$expire_date = new DateTime($expire);
//convert dates to seconds for easier comparison
$expire_secs = $expire_date->format('U');
$today_secs = $today->format('U');
//do the comparison
if( $expire_secs < $today_secs ) {
//update the field for that post ACF
$status = 'false';
update_field( 'featured', $status, $post_id );
//non ACF
update_post_meta($post_id, 'featured', $status);
}
}
}
}
add_action( 'jj_feaurecheck_cron', 'jj_feaurecheck_cron_function' );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/454867.html
上一篇:如何在WooCommerce商店和存檔頁面上為產品圖片添加類
下一篇:通過WinAPI截屏(安全)
