我创建了一些运行cron作业的代码,以查看我分配给我的帖子(自定义字段)的到期日期是否得到满足。如果是这样,它会将状态更改为"草稿"。
我的日程安排出了问题,因为cron工作每分钟都在排队,我想我已经分配了它每天运行一次。
任何帮助都非常感激。
function cronstarter_activation()
{
if( !wp_next_scheduled("katie-posts-cron-job"))
{
wp_schedule_event (time(), "daily", "katie-posts-cronjob");
}
}
add_action("wp", "cronstarter_activation");
function cronstarter_deactivate()
{
$timestamp = wp_next_scheduled ("katie-posts-cronjob");
wp_unschedule_event ($timestamp, "katie-posts-cronjob");
}
register_deactivation_hook (__FILE__, "cronstarter_deactivate");
function cron_add_timer($schedules)
{
$schedules["everyday"] = array(
'interval' => 86400,
'display' => __("Once a Day")
);
return $schedules;
}
add_filter("cron_schedules", "cron_add_timer");
function prfx_custom_meta()
{
add_meta_box ("prfx_meta", __("katie Posts", "prfx-textdomain"), "prfx_meta_callback", "post", "normal", "high");
}
add_action ("add_meta_boxes", "prfx_custom_meta");
function prfx_admin_styles()
{
global $typenow;
if ($typenow == "post")
{
wp_enqueue_style ("prfx_meta_box_styles", plugin_dir_url( __FILE__ ) . "/css/meta-box-styles.css");
}
}
add_action ("admin_print_styles", "prfx_admin_styles");
function prfx_meta_callback($post)
{
wp_nonce_field (basename( __FILE__ ), "prfx_nonce");
$prfx_stored_meta = get_post_meta( $post->ID );
?>
<p>
<label for="meta-text" class="prfx-row-title"><?php _e("Post Expiry Date: ", "prfx-textdomain")?></label>
<input type="date" name="meta-text" id="meta-text" value="<?php if (isset ($prfx_stored_meta["meta-text"])) echo $prfx_stored_meta["meta-text"][0]; ?>" />
</p>
<?php
}
function prfx_meta_save($post_id)
{
$is_autosave = wp_is_post_autosave($post_id);
$is_revision = wp_is_post_revision($post_id);
$is_valid_nonce = (isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( $_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
if ($is_autosave || $is_revision || !$is_valid_nonce)
{
return;
}
if (isset($_POST["meta-text"]))
{
update_post_meta($post_id, "meta-text", sanitize_text_field($_POST["meta-text"]));
}
}
add_action ("save_post", "prfx_meta_save");
function katie_expired_posts()
{
$args = array ("post_type" => "post", "post_status" => "publish");
$post_query = new WP_Query($args);
if ($post_query->have_posts())
{
while($post_query->have_posts() )
{
$post_query->the_post();
$sExpiry = get_post_meta (get_the_ID(), "meta-text", true);
$sToday = date("Y-m-d");
if ( ($sExpiry <= $sToday) && ($sExpiry != "" ) )
{
$my_post = array ("ID" => get_the_ID(), "post_status" => "Draft",);
wp_update_post ($my_post);
}
}
}
}
add_action ("katie-posts-cronjob", "katie_expired_posts");
答案 0 :(得分:0)
您的日程安排命名不正确。
请参阅:
function cronstarter_activation()
{
if( !wp_next_scheduled("katie-posts-cron-job")) // <--- cron-job
{
wp_schedule_event (time(), "daily", "katie-posts-cronjob"); // <--- cronjob
}
}
问候,Bjorn