When building reusable layouts in Etch, a query should rarely be treated as a one-off block of code. A better approach is to make the query flexible enough to accept a few controlled values while still keeping safe defaults in place.
This pattern is useful when you want one query to support different counts, sort orders, taxonomy filters, and fallback behavior without rewriting the query each time.
$query_args = [
'post_type' => 'resource',
'posts_per_page' => $count ?? 10,
'post_status' => 'publish',
'orderby' => $sort ?? 'date',
'order' => $dir ?? 'DESC',
'tax_query' => [
[
'taxonomy' => $cat ?? 'resource-category',
'field' => 'slug',
'terms' => $name ?? 'general'
]
]
];What This Query Controls
The query is designed to retrieve published items from a specific post type and optionally filter them by taxonomy term. It also allows several values to be passed in dynamically.
- The number of posts to return
- The field used for sorting
- The sort direction
- The taxonomy used for filtering
- The term used in the taxonomy filter
Why the Fallbacks Matter
Each dynamic value has a fallback. This means the query can still run even when no custom value is passed into it.
For example, if no count is provided, the query can fall back to a default number of posts. If no sort value is provided, it can fall back to ordering by date. This keeps the query predictable and prevents small missing values from breaking the output.
Using Etch Variables
Etch uses the $ syntax to pass values into query arguments. This makes it possible for a component or layout to control parts of the query without changing the query structure itself.
In this query, those variables are used only where flexibility is needed. The post type and post status remain fixed, while the count, sorting, and taxonomy filter can be adjusted.
Keeping the Query Focused
This query is intentionally narrow in scope. It is not trying to become a universal query builder. Instead, it supports a specific content type and a specific filtering pattern.
That is usually the better approach for reusable components. The more open-ended a query becomes, the easier it is to introduce unexpected behavior.
Filtering by Taxonomy
The taxonomy filter is the most important part of this pattern. It allows the query to return posts that belong to a selected term, such as a resource category.
By making the taxonomy and term values dynamic, the same query can be reused in different contexts. One instance might show general resources, while another might show resources from a more specific category.
Passing Multiple Values
One benefit of this approach is that some query arguments can accept multiple values. The taxonomy term value, for example, can be passed as a single term or as multiple terms.
This gives the query more flexibility while keeping the overall structure the same. Instead of creating separate queries for each variation, the same query can support different term combinations.
When to Use This Pattern
This pattern works well when building reusable sections such as resource grids, featured content blocks, related content areas, or category-based listings.
It is especially useful when the layout should stay the same, but the returned content needs to change based on a few arguments.
A flexible query does not need to be complicated. By choosing a few arguments to make dynamic and assigning each one a sensible fallback, you can create a reusable, predictable, and easy-to-manage query in Etch.
Share Your Thoughts