Skip to content

Instantly share code, notes, and snippets.

@hissy
Created January 31, 2017 16:18
Show Gist options
  • Save hissy/1d8a421f2b168e98257dde44b63f3377 to your computer and use it in GitHub Desktop.
Save hissy/1d8a421f2b168e98257dde44b63f3377 to your computer and use it in GitHub Desktop.
[WordPress] Extended WP_Query implements Iterator
<?php
$args = ['post_type' => 'post'];
foreach((new Iterative_WP_Query($args)) as $post_id => $post) {
	echo get_the_title($post_id);
}
<?php
/*
Plugin Name: Iterative WP_Query
Author: Takuro Hishikawa
Version: 0.1
Author URI: http://notnil-creative.com
*/
class Iterative_WP_Query extends WP_Query implements Iterator
{
public function current()
{
return $this->post = $this->posts[$this->current_post];
}
public function next()
{
$this->current_post++;
}
public function key()
{
return $this->posts[$this->current_post]->ID;
}
public function valid()
{
return isset($this->posts[$this->current_post]);
}
public function rewind()
{
$this->current_post = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment