1

It seems like all the answers on here for this question end up being a syntax error, however I'm not seeing it here.

I'm trying to setup a daily job here that will run through all my 'vacation' posts, if the _simple_start_date (unix timestamp) is the same date as today, then run some functions to update the usermeta. The three functions I call are defined earlier in my plugin and I know work since as I am using them multiple other places to do the exact same thing I'm doing here. Only difference is that this is a scheduled event.

Since I have set this up, it has not ran once.

I am using define( 'DISABLE_WP_CRON', true ); for use with some other plugins I have that are running daily tasks. However from what I've read, that should not cause an issue with this.

Any help is of course greatly appreciated. thanks

add_action( 'wp', 'pto_setup_schedule' );
/**
 * On an early action hook, check if the hook is scheduled - if not, schedule it.
 */
function pto_setup_schedule() {
    if ( ! wp_next_scheduled( 'pto_daily_event' ) ) {
        wp_schedule_event( time(), 'daily', 'pto_daily_event');
    }
}


add_action( 'pto_daily_event', 'check_pto_request_date' );
/**
 * On the scheduled action hook, run a function.
 */
function check_pto_request_date() {

    $loop = new WP_Query( array( 'post_type' => 'vacation') ); 
    while ( $loop->have_posts() ) : $loop->the_post();

    $request_start_time = (get_post_meta( $post->ID, '_simple_start_date',true) );

    if  (date('Ymd') == date('Ymd', $request_start_time)){

        $current_user = $post->post_author;
        $request_date = get_post_meta( $post->ID, '_simple_start_date',true );
        $request_year = date("Y",$request_date);
        $time_year= date("Y");
        $request_time = get_post_meta($post->ID, '_simple_total_hours', true);
        $request_type = get_post_meta($post->ID, '_simple_vacation_type', true);

        update_days_requested((-1 * $request_time), $request_type, $current_user);
        update_days_left((-1 * $request_time), $request_type, $current_user);
        update_days_used($request_time, $request_type, $current_user);


    } 
    endwhile;



}
3
  • 1
    Using define('DISABLE_WP_CRON', true); disables WP cron jobs, why do you expect that scheduled events will still be working? Where have you read that define('DISABLE_WP_CRON', true); doesn't cause issues with scheduled events? If you disable WP Cron you need to set up a cron job at server level that executes wp-cron.php script if you want to use WP scheduled events.
    – cybmeta
    Mar 2, 2015 at 16:43
  • This is what I followed in the past which indicates to setup task using wp_schedule_event and then setup server cron job. tommcfarlin.com/wordpress-cron-jobs And I assumed that since I was using Disable_Wp_Cron for other plugins that are running daily tasks, that implied I had my cron job setup to work properly. So yes, I have a cron job that runs every morning that executes wp-cron.php Mar 2, 2015 at 16:51
  • So, everything looks OK.
    – cybmeta
    Mar 2, 2015 at 17:56

1 Answer 1

2

I think your problem may be in the use of $post->ID. In your code $post is an undefined variable. As you are inside a loop, try get_the_ID() instead of $post->ID:

$post_ID = get_the_ID();

$request_start_time = get_post_meta( $post_ID, '_simple_start_date', true );

if  (date('Ymd') == date('Ymd', $request_start_time)){

    $current_user = $post->post_author;
    $request_date = get_post_meta( $post_ID, '_simple_start_date',true );
    $request_year = date("Y",$request_date);
    $time_year= date("Y");
    $request_time = get_post_meta($post_ID, '_simple_total_hours', true);
    $request_type = get_post_meta($post_ID, '_simple_vacation_type', true);

    update_days_requested((-1 * $request_time), $request_type, $current_user);
    update_days_left((-1 * $request_time), $request_type, $current_user);
    update_days_used($request_time, $request_type, $current_user);


} 
3
  • ah. thanks. So if my $post object isn't going to work, then I guess $current_user = $post->post_author; is null as well? Could I also just include global $post inside the loop and then the $post object wouldn't be empty? Mar 2, 2015 at 18:28
  • Yes, you could try global $post as $loop->the_post() method setup the global $post to current post in the loop. In this case it is also recommended to run wp_reset_postdat() after the loop to restore global $post.
    – cybmeta
    Mar 2, 2015 at 19:34
  • great. thanks. I'll give it a go and see what happens. Mar 2, 2015 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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