A custom sortable admin column works fine most of the time, but sorting by it makes some posts disappear from the list entirely – not sort to one end, actually vanish.
⚡ Quick Fix (TL;DR)
The Culprit: Sorting a WP_Query by meta_key plus orderby: meta_value (or meta_value_num) uses an inner join on the postmeta table. Any post that doesn't have that meta key at all gets silently excluded from the result set – not sorted to one end, removed entirely. This happened twice independently on the same site: once for an existing Stage column, once for a brand-new column, both with the identical structural flaw.
The Fix: Add an explicit meta_query with an OR relation combining an EXISTS clause and a NOT EXISTS clause for the same key. This is WordPress's own documented pattern for keeping every post in the result set regardless of whether it has a value to sort by yet.
$query->set( 'meta_key', 'your_meta_key' );
$query->set( 'orderby', 'meta_value' ); // or meta_value_num
$query->set( 'meta_query', array(
'relation' => 'OR',
array( 'key' => 'your_meta_key', 'compare' => 'EXISTS' ),
array( 'key' => 'your_meta_key', 'compare' => 'NOT EXISTS' ),
) );Tagged in :
More from the field
Bisect to an Empty Baseline Before Trusting Any Single Theory
.
Multiple plausible-sounding theories for a bug each turned out to be wrong when actually tested – time was spent building fixes…
A Security Dashboard’s Activity Log Can Lag a Full Day Behind Real Time
.
Trying to check a security tool’s activity log for an event that had just happened showed nothing at all.