/** * @param mixed $query * @param string $post_class */ public function __construct($query = false, $post_class = '\\Timber\\Post') { $this->userQuery = $query; $this->queryIterator = PostGetter::query_posts($query, $post_class); $posts = $this->queryIterator->get_posts(); parent::__construct($posts, $post_class); }
/** * @param mixed $query * @param string|array $PostClass * @return array|bool|null */ public static function get_post($query = false, $PostClass = '\\Timber\\Post') { // if a post id is passed, grab the post directly if (is_numeric($query)) { $post_type = get_post_type($query); $PostClass = PostGetter::get_post_class($post_type, $PostClass); $post = new $PostClass($query); // get the latest revision if we're dealing with a preview $posts = PostsCollection::maybe_set_preview(array($post)); if ($post = reset($posts)) { return $post; } } $posts = self::get_posts($query, $PostClass); if ($post = reset($posts)) { return $post; } }
protected static function init($posts, $post_class) { $returned_posts = array(); if (is_null($posts)) { $posts = array(); } foreach ($posts as $post_object) { $post_type = get_post_type($post_object); $post_class_use = PostGetter::get_post_class($post_type, $post_class); // Don't create yet another object if $post_object is already of the right type if (is_a($post_object, $post_class_use)) { $post = $post_object; } else { $post = new $post_class_use($post_object); } if (isset($post->ID)) { $returned_posts[] = $post; } } return self::maybe_set_preview($returned_posts); }
public function __construct($posts = array(), $post_class = '\\Timber\\Post') { $returned_posts = array(); if (is_null($posts)) { $posts = array(); } foreach ($posts as $post_object) { $post_type = get_post_type($post_object); $post_class_use = PostGetter::get_post_class($post_type, $post_class); // Don't create yet another object if $post_object is already of the right type if (is_a($post_object, $post_class_use)) { $post = $post_object; } else { $post = new $post_class_use($post_object); } if (isset($post->ID)) { $returned_posts[] = $post; } } $returned_posts = self::maybe_set_preview($returned_posts); parent::__construct($returned_posts, $flags = 0, 'Timber\\PostsIterator'); }
/** * tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through) * @internal * @param mixed a value to test against * @return int the numberic id we should be using for this post object */ protected function determine_id($pid) { global $wp_query; if ($pid === null && isset($wp_query->queried_object_id) && $wp_query->queried_object_id && isset($wp_query->queried_object) && is_object($wp_query->queried_object) && get_class($wp_query->queried_object) == 'WP_Post') { if (isset($_GET['preview']) && isset($_GET['preview_nonce']) && wp_verify_nonce($_GET['preview_nonce'], 'post_preview_' . $wp_query->queried_object_id)) { $pid = $this->get_post_preview_id($wp_query); } else { if (!$pid) { $pid = $wp_query->queried_object_id; } } } else { if ($pid === null && $wp_query->is_home && isset($wp_query->queried_object_id) && $wp_query->queried_object_id) { //hack for static page as home page $pid = $wp_query->queried_object_id; } else { if ($pid === null) { $gtid = false; $maybe_post = get_post(); if (isset($maybe_post->ID)) { $gtid = true; } if ($gtid) { $pid = get_the_ID(); } if (!$pid) { global $wp_query; if (isset($wp_query->query['p'])) { $pid = $wp_query->query['p']; } } } } } if ($pid === null && ($pid_from_loop = PostGetter::loop_to_id())) { $pid = $pid_from_loop; } return $pid; }
/** * Query posts. * @api * @param mixed $query * @param string $PostClass * @return array|bool|null */ public static function query_posts($query = false, $PostClass = 'Timber\\Post') { return PostGetter::query_posts($query, $PostClass); }
public function findAll($args = []) { $args = array_merge(['posts_per_page' => '-1', 'post_type' => $this->getPostType()], $args); return Timber\PostGetter::get_posts($args, get_called_class()); }