0

I am trying to clean up my codes and separate them in class files.

I have this block of code that works perfectly fine where it sets the status of events older than a week to trash when I add it in the functions.php file:

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 );
   }

 }  

But when I try to convert this to a class, when I try to run the CRON job in the admin area, it doesn't work.

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();

Is there something that I am missing in the class implementation why it is not working? I tried searching around but I am not able to find any clear reference.

  • 2
    update_events() needs to be a public method. – Nathan Johnson Mar 29 '18 at 23:08
  • @NathanJohnson you should post answers as answers, no rep earned from upvoting comments! – Tom J Nowell Mar 29 '18 at 23:43
2

As Nathan has said in his comment above, the update_events() function needs to be a public function, not a private function:

  • public scope to make that variable/function available from anywhere, other classes and instances of the object.
  • private scope when you want your variable/function to be visible in its own class only.
  • protected scope when you want to make your variable/function visible in all classes that extend current class including the parent class.
  • I would add that PHP should be throwing warnings about this in your error log too – Tom J Nowell Mar 29 '18 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.