Celeste includes a powerful Query Builder to make working with WordPress core queries easier and (hopefully) less painful.

Imagine you want to query for your latest posts and include the featured image, permalink and tags. This is what that might look like:

<?php

$posts = get_posts();

if ($posts) {
	foreach ($posts as $post) {
		$post->featured_image = get_the_post_thumbnail_url($post->ID);
		$post->permalink = get_permalink($post->ID);
		$post->tags = get_the_tags($post->ID, 'post_tag');
	}
}

Lots of boilerplate, and not always the data you want. For example, get_the_post_thumbnail_url will only return one size. What if you need more?

That’s where Celeste’s Query Builder comes in. That same query looks like this:

<?php

use Celeste\\Query\\Post;

$posts = Post::query()
	->with_featured_image()
	->with_tags()
	->get();

More examples →

<aside> 👉 The Query Builder augments the WP_Post object and modifies it to remove the post_ prefix on each property. As such, it returns Celeste\\Models\\Post objects instead, which include some handy methods of it’s own. See the documentation for the Post model for more info.

</aside>

Available Methods

Celeste’s Query Builder offers an array of methods for querying posts. These methods may be chained together (with the exception of query and id, which must be placed at the start of a query) to create queries.

From here on, it’s assumed that you have included the use Celeste\\Query\\Post; statement as shown above.

Query a Single Post

id

If you need to query a single post, you may pass an ID to the id method. You do not need to start the chain with query .

<aside> ℹ️ Note that when querying for a post ID, the filter methods have no effect. You may still use Query Modifiers, however.

</aside>

Post::id(234)->get();

Query Multiple Posts