• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timber / timber / 20695674007

04 Jan 2026 04:14PM UTC coverage: 89.681% (+1.5%) from 88.211%
20695674007

push

travis-ci

nlemoine
test: Fix ancestors post tests

4615 of 5146 relevant lines covered (89.68%)

63.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.33
/src/PostQuery.php
1
<?php
2

3
namespace Timber;
4

5
use ArrayObject;
6
use JsonSerializable;
7
use ReturnTypeWillChange;
8
use WP_Query;
9

10
/**
11
 * Class PostQuery
12
 *
13
 * Query for a collection of WordPress posts.
14
 *
15
 * This is the equivalent of using `WP_Query` in normal WordPress development.
16
 *
17
 * PostQuery is used directly in Twig templates to iterate through post query results and
18
 * retrieve meta information about them.
19
 *
20
 * @api
21
 */
22
class PostQuery extends ArrayObject implements PostCollectionInterface, JsonSerializable
23
{
24
    use AccessesPostsLazily;
25

26
    /**
27
     * Found posts.
28
     *
29
     * The total amount of posts found for this query. Will be `0` if you used `no_found_rows` as a
30
     * query parameter. Will be `null` if you passed in an existing collection of posts.
31
     *
32
     * @api
33
     * @since 1.11.1
34
     * @var int The amount of posts found in the query.
35
     */
36
    public $found_posts = null;
37

38
    /**
39
     * If the user passed an array, it is stored here.
40
     *
41
     * @var array
42
     */
43
    protected $userQuery;
44

45
    /**
46
     * The internal WP_Query instance that this object is wrapping.
47
     *
48
     * @var WP_Query
49
     */
50
    protected $wp_query = null;
51

52
    protected $pagination = null;
53

54
    /**
55
     * Query for a collection of WordPress posts.
56
     *
57
     * Refer to the official documentation for
58
     * [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) for a list of all
59
     * the arguments that can be used for the `$query` parameter.
60
     *
61
     * @api
62
     * @example
63
     * ```php
64
     * // Get posts from default query.
65
     * global $wp_query;
66
     *
67
     * $posts = Timber::get_posts( $wp_query );
68
     *
69
     * // Using the WP_Query argument format.
70
     * $posts = Timber::get_posts( [
71
     *     'post_type'     => 'article',
72
     *     'category_name' => 'sports',
73
     * ] );
74
     *
75
     * // Passing a WP_Query instance.
76
     * $posts = Timber::get_posts( new WP_Query( [ 'post_type' => 'any' ) );
77
     * ```
78
     *
79
     * @param WP_Query $query The WP_Query object to wrap.
80
     */
81
    public function __construct(WP_Query $query)
100✔
82
    {
83
        $this->wp_query = $query;
100✔
84
        $this->found_posts = (int) $this->wp_query->found_posts;
100✔
85

86
        $posts = $this->wp_query->posts ?: [];
100✔
87

88
        parent::__construct($posts, 0, PostsIterator::class);
100✔
89
    }
90

91
    /**
92
     * Get pagination for a post collection.
93
     *
94
     * Refer to the [Pagination Guide]({{< relref "../guides/pagination.md" >}}) for a detailed usage example.
95
     *
96
     * Optionally could be used to get pagination with custom preferences.
97
     *
98
     * @api
99
     * @example
100
     * ```twig
101
     * {% if posts.pagination.prev %}
102
     *     <a href="{{ posts.pagination.prev.link }}">Prev</a>
103
     * {% endif %}
104
     *
105
     * <ul class="pages">
106
     *     {% for page in posts.pagination.pages %}
107
     *         <li>
108
     *             <a href="{{ page.link }}" class="{{ page.class }}">{{ page.title }}</a>
109
     *         </li>
110
     *     {% endfor %}
111
     * </ul>
112
     *
113
     * {% if posts.pagination.next %}
114
     *     <a href="{{ posts.pagination.next.link }}">Next</a>
115
     * {% endif %}
116
     * ```
117
     *
118
     * @param array $prefs Optional. Custom preferences. Default `array()`.
119
     *
120
     * @return Pagination object
121
     */
122
    public function pagination($prefs = [])
23✔
123
    {
124
        if (!$this->pagination && $this->wp_query instanceof WP_Query) {
23✔
125
            $this->pagination = new Pagination($prefs, $this->wp_query);
23✔
126
        }
127

128
        return $this->pagination;
23✔
129
    }
130

131
    /**
132
     * Gets the original query used to get a collection of Timber posts.
133
     *
134
     * @since 2.0
135
     * @return WP_Query|null
136
     */
137
    public function query(): ?WP_Query
1✔
138
    {
139
        return $this->wp_query;
1✔
140
    }
141

142
    /**
143
     * Gets the original query used to get a collection of Timber posts.
144
     *
145
     * @deprecated 2.0.0, use PostQuery::query() instead.
146
     * @return WP_Query|null
147
     */
148
    public function get_query(): ?WP_Query
1✔
149
    {
150
        Helper::deprecated('Timber\PostQuery::get_query()', 'Timber\PostQuery::query()', '2.0.0');
1✔
151

152
        return $this->wp_query;
1✔
153
    }
154

155
    /**
156
     * Override data printed by var_dump() and similar. Realizes the collection before
157
     * returning. Due to a PHP bug, this only works in PHP >= 7.4.
158
     *
159
     * @see https://bugs.php.net/bug.php?id=69264
160
     * @internal
161
     */
162
    public function __debugInfo(): array
×
163
    {
164
        return [
×
165
            'info' => \sprintf(
×
166
                '
×
167
********************************************************************************
168

169
    This output is generated by %s().
170

171
    The properties you see here are not actual properties, but only debug
172
    output. If you want to access the actual instances of Timber\Posts, loop
173
        over the collection or get all posts through $query->to_array().
174

175
        More info: https://timber.github.io/docs/v2/guides/posts/#debugging-post-collections
176

177
********************************************************************************',
×
178
                __METHOD__
×
179
            ),
×
180
            'posts' => $this->getArrayCopy(),
×
181
            'wp_query' => $this->wp_query,
×
182
            'found_posts' => $this->found_posts,
×
183
            'pagination' => $this->pagination,
×
184
            'factory' => $this->factory,
×
185
            'iterator' => $this->getIterator(),
×
186
        ];
×
187
    }
188

189
    /**
190
     * Returns realized (eagerly instantiated) Timber\Post data to serialize to JSON.
191
     *
192
     * @internal
193
     */
194
    #[ReturnTypeWillChange]
3✔
195
    public function jsonSerialize()
196
    {
197
        return $this->getArrayCopy();
3✔
198
    }
199
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc