• I am currently making a cron that is supposed to run once a day and adds a 1 to the post meta. I have tested the function to run on a page load and it does this correctly. However, when I tie it to a cron event, every time the page loads it adds 4 to the total. I do not understand why this is happening. I called my hosting site and they say it should only be running once a day. Here is the code:

    /*Adds a day to paused days meta.*/
    
    $time = rand(1000, 1200);
    
    if ( ! wp_next_scheduled( 'update_pused' ) ) {
      wp_schedule_event( time()+$time, 'daily', 'update_paused' );
    }
    
    add_action( 'update_paused', 'add_day_to_paused' );
    
    function add_day_to_paused() {
    	global $woocommerce;
        $args = array(
    		'post_type'			=> 'shop_order',
    		'post_status' 		=> 'publish',
                    'posts_per_page' => -1,
    		'tax_query' => array(
                        array(
                            'taxonomy' => 'shop_order_status',
                            'field' => 'slug',
                            'terms' => array('paused')
                        )
                    )
    	);
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		$order_id = $loop->post->ID;
    
      		$order = new WC_Order($order_id);
    
      			$getdays = get_post_meta($order_id,'paused_days', true);
            	$totaldays = 1 + $getdays;
            	update_post_meta($order_id,'paused_days',$totaldays);
    
      endwhile;
    
    }
  • The topic ‘Cron/Plugin error’ is closed to new replies.