Smallest Example#
Use sort="field:direction" on the query tag:
{{ query:posts sort="date:desc" }} <h2>{{ title }}</h2>{{ /query:posts }}date:desc shows the newest posts first. Use date:asc for oldest first.
Manual Order#
Use order:asc when editors arrange pages or post types manually in the WordPress admin:
{{ query:pages sort="order:asc" }} <h2>{{ title }}</h2>{{ /query:pages }}order is the TemplateX sort name for WordPress menu_order. Use order:asc for first-to-last manual order and order:desc for the reverse.
Do not write menu_order:asc in TemplateX templates. menu_order is the WordPress field name; TemplateX syntax uses order.
Alphabetical#
Use title:asc for A to Z:
{{ query:posts sort="title:asc" }} <h2>{{ title }}</h2>{{ /query:posts }}Use title:desc for Z to A.
Random#
Use random when the query should shuffle results:
{{ query:posts sort="random" }} <h2>{{ title }}</h2>{{ /query:posts }}Random sorting is useful for featured links, related posts, or rotating highlights.
Multiple Sorts#
Separate sort rules with | when one field should break ties for another:
{{ query:posts sort="order:asc|title:asc" }} <h2>{{ title }}</h2>{{ /query:posts }}TemplateX applies sort rules from left to right. In this example, WordPress uses manual order first, then title order for posts with the same manual position.
Sort Reference#
| Sort value | WordPress order | Use for |
|---|---|---|
date:desc | date DESC | Newest posts first. |
date:asc | date ASC | Oldest posts first. |
published:desc | date DESC | Same as date:desc. |
modified:desc | modified DESC | Recently updated posts first. |
title:asc | title ASC | Alphabetical titles. |
slug:asc | name ASC | Post slug order. |
order:asc | menu_order ASC | Manual WordPress order. |
order:desc | menu_order DESC | Reverse manual WordPress order. |
comment_count:desc | comment_count DESC | Most-commented posts first. |
status:asc | post_status ASC | Group by post status. |
random | rand | Random results. |
If the field is not one of the names above, TemplateX treats it as a custom field key and sorts by WordPress meta_value:
{{ query:events sort="event_date:asc" }} <h2>{{ title }}</h2>{{ /query:events }}Custom field sorting uses WordPress text-style meta sorting. Store values in a sortable format, such as 2026-06-14 for dates.
Reader-Chosen Sorting#
Use search sorting when a reader should choose the order from the page:
{{ search }} {{ sort }} <option value="date:desc">Newest</option> <option value="order:asc">Menu Order</option> <option value="title:asc">Title A-Z</option> {{ /sort }}{{ /search }}{{ query:posts search }} <h2>{{ title }}</h2>{{ /query:posts }}Search sort options use the same sort values as query loops. See Search Sorting for the form syntax.