<?php
$args = ['post_type' => 'post'];
foreach((new Iterative_WP_Query($args)) as $post_id => $post) {
echo get_the_title($post_id);
}
Created
January 31, 2017 16:18
-
-
Save hissy/1d8a421f2b168e98257dde44b63f3377 to your computer and use it in GitHub Desktop.
[WordPress] Extended WP_Query implements Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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