gdarko/wp-batch-processing

The Batch keep running over 100%

gianghl1983 opened this issue · 2 comments

Hi,

I am trying with simple delete post like this, but the batch keep running even reach 100%...

Please check that...!

public function setup() {

		$posts = get_posts( array(
			'numberposts'      => 10,
			'post_type'   => 'it24-serial-number'
		) );

		foreach ( $posts as $post ) {
			$this->push( new WP_Batch_Item( $post->ID, array( 'post_id' => $post->ID ) ) );
		}
	}

public function process( $item ) {

		// Retrieve the custom data
		$post_id = $item->get_value( 'post_id' );
		

		// Return WP_Error if the item processing failed (In our case we simply skip author with user id 5)
		// if ( $post_id != 5 ) {
		// 	return new WP_Error( 302, "ID".$post_id );
		// }

		// Do the expensive processing here. eg. Sending email.
		// ...
		**wp_trash_post($post_id);**
		
		// Return true if the item processing is successful.
		return true;
	}

	/**
	 * Called when specific process is finished (all items were processed).
	 * This method can be overriden in the process class.
	 * @return void
	 */
	public function finish() {
		// Do something after process is finished.
		// You have $this->items, or other data you can set.
	}

}

Same here.

I have been having this issue too. I am updating ~3000 posts, only if they do not have a certain meta value. Once they are updated that meta value is added. When it updates the Total # goes down.

Problem is that every time it runs to update the number processed it runs the setup method again and the query again which then updates the total number. The % then takes the Processed # / Total # to get the % complete. It does what it's supposed to do, but the visual feedback is then incorrect.

I guess the fix, be it when you are creating the batch, or something the author of the plugin adds, is to make sure whatever query you run in setup is only ran once and saved.