我正在尝试清理代码并在类文件中分离它们。
我有一段代码,当我将其添加到functions.php 文件:
if ( ! wp_next_scheduled( \'trash_week_old_events\' ) ) {
wp_schedule_event( time(), \'daily\' );
}
add_action( \'trash_week_old_events\', \'trash_events\' );
function trash_events() {
$args = array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'date_query\' => array(
array(
\'before\' => \'1 week ago\'
)
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$updated_post_info = array(
\'ID\' => $post->ID,
\'post_status\' => \'trash\'
);
wp_update_post( $updated_post_info );
}
}
但是,当我尝试将其转换为类时,当我尝试在管理区域中运行CRON作业时,它不起作用。
class EventsWPCron {
public function __construct() {
if ( ! wp_next_scheduled( \'trash_week_old_events\' ) ) {
wp_schedule_event( time(), \'daily\' );
}
add_action( \'trash_week_old_events\', array( $this, \'update_events\' ) );
}
private function update_events() {
$args = array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'date_query\' => array(
array(
\'before\' => \'1 week ago\'
)
)
);
$query = new WP_Query( $args );
$posts = $query->get_posts();
foreach( $posts as $post ) {
$updated_post_info = array(
\'ID\' => $post->ID,
\'post_status\' => \'trash\'
);
wp_update_post( $updated_post_info );
}
}
}
new EventsWPCron();
我在类实现中是否缺少一些东西,为什么它不起作用?我试着四处搜索,但找不到任何明确的参考资料。