Symptom
The archive page for your custom taxonomy loads. The posts appear. The pagination works. But the order is wrong: it does not follow the custom field you set as the sort key. Sometimes the posts come out in descending ID order, sometimes in whatever order the database returned them, sometimes in an order that looks almost right until you check a specific post.
The template file is not the problem. The archive order is a property of the main query’s arguments. If the rendered order is wrong, the first question is which query the loop is iterating, not what the template prints.
Trace: Which Query Is Producing the Loop?
WordPress fires pre_get_posts on every WP_Query instance, not just the main one. The hook documentation is explicit: it fires after the query variable object is created but before the actual query is run, and it receives the WP_Query instance by reference. That means a callback that sets meta_key and orderby without checking which query it is modifying can easily hit a secondary query in a sidebar, a related-posts block, or a widget. The main archive query then runs with its default arguments and renders in default order.
The documentation also warns that without conditionals like is_admin() and $query->is_main_query(), a callback can unintentionally change the query for custom loops in sidebars, footers, or elsewhere. The inverse is also true: a callback that does not target the main query will not change the archive.
First diagnostic: confirm which WP_Query instance is producing the archive loop. The main query is the one the primary loop uses. If your callback is not checking $query->is_main_query(), it is probably not the query you think it is.
Trace: Is the Callback Targeting the Right Query?
Inside pre_get_posts, use the method versions on the passed $query instance, not the global helper functions. The hook documentation states this directly: is_main_query() and similar functions test against the global $wp_query instance, not the passed one. A callback that calls the global is_tax() or is_main_query() can therefore make decisions about the wrong query.
A correct targeting pattern for a custom taxonomy archive looks like this:
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( ! $query->is_tax( 'your_taxonomy' ) ) {
return;
}
$query->set( 'meta_key', 'your_sort_field' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
} );
Two things to note. First, $query->is_tax( 'your_taxonomy' ) is a method on the passed instance, not the global helper. Second, the callback returns early for admin requests and for any query that is not the main query. That prevents the sort arguments from leaking into secondary loops.
The pre_get_posts documentation includes a similar example for a post type archive: it sets meta_key, orderby to meta_value_num, order to ASC, and a meta_query clause on the main query for an event post type. The pattern is the same; only the conditional changes.
Trace: Are the Sort Arguments Reaching the Query?
If the callback is targeting the main taxonomy archive query and the order is still wrong, the next step is to inspect the query vars on the main query. The WP_Query reference documents Custom Field (post meta) Parameters and Order & Orderby Parameters as argument groups. If meta_key and orderby are not present in the query vars, the callback is not firing on the right query or is being overridden later.
A quick inspection inside the callback, or in a temporary mu-plugin, prints the relevant vars:
add_action( 'pre_get_posts', function ( $query ) {
if ( ! $query->is_main_query() || is_admin() ) {
return;
}
if ( ! $query->is_tax( 'your_taxonomy' ) ) {
return;
}
error_log( 'meta_key: ' . var_export( $query->get( 'meta_key' ), true ) );
error_log( 'orderby: ' . var_export( $query->get( 'orderby' ), true ) );
error_log( 'order: ' . var_export( $query->get( 'order' ), true ) );
}, 99 );
If those values are empty at priority 99, something earlier in the request is resetting them, or the callback is not running on the query that renders the archive. If they are present but the rendered order is still wrong, the problem is downstream: the query is receiving the arguments but the database is not sorting the way you expect.
Root Cause: The Main Query Never Received the Sort Arguments
The most common failure mode is not a core bug in meta sorting. It is that the main taxonomy archive query never received the meta_key and orderby arguments. The callback either did not target the main query, did not run on the taxonomy archive, or was overridden by a later callback.
A hand-built WP_Query with meta_key and orderby proves the sort arguments are syntactically acceptable. It does not prove the main archive query is receiving them. Treating the two as the same query is the most common false lead in this failure mode. A custom query in a template or a shortcode is a different query from the main archive query; the archive only changes if you modify the main query.
There is a second, less common cause: the custom field is missing on some posts. The WP_Query reference documents the meta parameters, but it does not promise that posts without the meta key will be included in a meta-sorted result. If the sort key is absent on some posts, those posts may be excluded from the result entirely, or ordered in a way that depends on the generated SQL join. That is a data problem, not a targeting problem, and it should be diagnosed separately.
Fix: Target the Main Query and Set the Arguments
The fix has two parts. First, target the main taxonomy archive query using the passed $query instance. Second, set meta_key, orderby, and order on that query.
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( ! $query->is_tax( 'your_taxonomy' ) ) {
return;
}
$query->set( 'meta_key', 'your_sort_field' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
} );
If the field contains numeric values, use meta_value_num instead of meta_value. The WP_Query reference documents both as orderby values, and the pre_get_posts example uses meta_value_num for a date field. Using meta_value on numeric data sorts lexicographically, which is a different order from numeric sorting.
If you need to restrict the archive to posts that have the field, add a meta_query clause with a compare of EXISTS. That makes the exclusion explicit rather than relying on the join behavior. The pre_get_posts example shows a meta_query clause alongside meta_key and orderby; the same structure applies here.
Verification: Check the Query, Not the Page
A page that looks sorted can be sorted by coincidence of insertion order. A page that looks unsorted can be a targeting failure or an argument failure, and only the query tells you which. Verify against the actual query vars or the generated SQL, not against the rendered page.
Inside the callback, after setting the arguments, log the query vars:
add_action( 'pre_get_posts', function ( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( ! $query->is_tax( 'your_taxonomy' ) ) {
return;
}
$query->set( 'meta_key', 'your_sort_field' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'ASC' );
error_log( 'meta_key: ' . var_export( $query->get( 'meta_key' ), true ) );
error_log( 'orderby: ' . var_export( $query->get( 'orderby' ), true ) );
}, 99 );
If the values are present at priority 99, the query is receiving the arguments. If the rendered order is still wrong, inspect the generated SQL. The WP_Query reference documents the request filter, which exposes the SQL string. A temporary mu-plugin can log it:
add_filter( 'posts_request', function ( $sql, $query ) {
if ( ! $query->is_main_query() || is_admin() ) {
return $sql;
}
if ( ! $query->is_tax( 'your_taxonomy' ) ) {
return $sql;
}
error_log( $sql );
return $sql;
}, 10, 2 );
Look for the JOIN to wp_postmeta and the ORDER BY clause. If the join is present and the order clause references the meta value, the query is doing what you asked. If the join is absent, the meta_key did not reach the query. If the join is present but the order clause is not, the orderby did not reach the query.
What About the Gist That Says to Remove post_type?
There is a widely shared snippet that claims including the post_type parameter in a WP_Query with tax_query and meta_key/orderby makes the sort fail, and that removing post_type fixes it. That is an unverified anecdote from a single snippet’s comment, not a documented rule. It should be treated as a hypothesis to test in a specific install, not as a fix to apply. The retrieved source does not support generalizing it, and the WP_Query reference documents post_type and tax_query as independent parameters.
If you are debugging a specific install and the gist’s pattern seems to apply, test it by logging the generated SQL with and without post_type. If the SQL changes in a way that explains the order, you have a reproducible finding for that install. If it does not, the gist is not the explanation.
Pagination Interaction
The pre_get_posts documentation warns that using the offset argument in any WordPress query can break pagination, and that preserving pagination with offset requires handling pagination manually. If you add offset alongside a meta sort, expect pagination to need manual handling. That is a separate failure mode from the sort itself, but it often appears in the same debugging session because both involve modifying the main query.
Term-Level Sorting Is a Different Problem
If the intent is to sort the terms in a taxonomy list, not the posts in a taxonomy archive, the relevant query is WP_Term_Query, not WP_Query. The WP_Term_Query reference documents that orderby accepts meta_value, meta_value_num, and the value of $meta_key, among other options. The get_terms() reference documents the meta_key and meta_value parameters and the ability to order results by metadata. That is a different query path from the post archive, and the fix belongs in the term query, not in pre_get_posts on the main post query.
FAQ
Why does my custom WP_Query sort correctly but the archive does not?
Because they are different queries. A hand-built WP_Query with meta_key and orderby proves the arguments are syntactically acceptable. The archive only changes if you modify the main query. If the archive is not sorted, the main query is not receiving the arguments.
Should I use meta_value or meta_value_num?
Use meta_value_num for numeric data and meta_value for text. The WP_Query reference documents both as orderby values. Using meta_value on numeric data sorts lexicographically, which is a different order from numeric sorting.
What happens to posts that do not have the custom field?
The WP_Query reference documents the meta parameters but does not promise that posts without the meta key will be included in a meta-sorted result. If the sort key is absent on some posts, those posts may be excluded from the result entirely, or ordered in a way that depends on the generated SQL join. If you need to include them, add a meta_query clause with a compare of EXISTS or handle the missing values explicitly.
Why does the order change when I add offset?
The pre_get_posts documentation warns that using offset in any WordPress query can break pagination, and that preserving pagination with offset requires handling pagination manually. That is a separate failure mode from the sort itself.
Is this a core bug?
The retrieved sources do not support a claim that meta sorting on a taxonomy archive is broken in WordPress core. The supported failure mode is that the main query is not receiving the sort arguments, or the callback is targeting the wrong query. Fix the targeting first, then the sort arguments, then verify against the actual query.