Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Wordpress

WordPress: Fix a WP_Query meta_query That Silently Returns the Wrong Posts Because Multiple Clauses Default to AND Without a Relation Key

- 11.10.26 - ErcanOPAK

📝 The meta_query That Assumed AND Without Asking

A meta_query array with two or more clauses needs an explicit relation key to say how those clauses should be combined, and leaving that key out doesn’t leave the behavior undefined – WP_Query quietly defaults to AND. A query meant to find listings that are either featured or sold, with no relation key ever set, silently becomes a search for listings that are both featured and sold at the same time, which on most sites is a rare or nonexistent combination, so the query just returns far fewer results than expected, or none.

🔎 The Problem

<?php
$query = new WP_Query( array(
    'post_type'  => 'listing',
    'meta_query' => array(
        array(
            'key'   => 'featured',
            'value' => '1',
        ),
        array(
            'key'   => 'status',
            'value' => 'sold',
        ),
    ),
) );
// Intent (implicit, never stated): "featured listings OR sold
// listings." Actual: WP_Query defaults an unlabeled multi-clause
// meta_query to 'relation' => 'AND', so this silently returns
// only listings that are BOTH featured AND sold at once.

✅ Fix: Always Set relation Explicitly on Multi-Clause meta_query

  • Add an explicit ‘relation’ => ‘OR’ (or ‘AND’, whichever is actually intended) as its own top-level entry inside meta_query any time there are two or more clauses – never rely on the default, and flag its absence in review for any meta_query with multiple clauses.
  • When a meta_query’s result count looks suspiciously low, check for a missing relation key before assuming the underlying post meta itself is wrong – a query silently running as AND when OR was intended is a far more common cause of “too few results” than the stored data actually being incomplete.
  • For anything beyond two simple clauses, nest meta_query arrays explicitly – an inner array with its own relation, combined with an outer relation – rather than relying on a flat list of clauses and an assumption about how they combine; a flat list’s combining rule becomes genuinely ambiguous to a future reader once there are three or more clauses.

⚠️ Why This Is Easy to Miss

  • The query runs without any PHP warning or WordPress debug notice – an incorrect default relation produces a perfectly well-formed, successfully executing SQL query, it just isn’t the query anyone actually intended to write.
  • A meta_query with exactly one clause behaves identically regardless of the relation key, since there’s nothing yet to combine – the bug only appears the moment a second clause is added later, often by a different developer with no reason to suspect the original single-clause query’s behavior is about to change.

meta_query doesn’t ask how you want two conditions combined – it assumes AND, silently, and the documentation has to be read specifically to find out that’s the default.

— WordPress Admin

Related posts:

WordPress: Fix a Block Editor Autosave That Silently Overwrites Your Manual Save Because Two Browser...

WordPress: Fix a Block Editor Autosave That Silently Overwrites Your Manual Save Because Two Browser...

WordPress: Use WP-CLI to Manage 100 Sites in 10 Minutes

WordPress: Use WP-CLI to Manage 100 Sites in 10 Minutes

WordPress: Optimize Database for Speed

WordPress: Optimize Database for Speed

Views: 8

Post navigation

C#: Fix a Static Readonly List That Gets Mutated From Anywhere Because readonly Only Locks the Reference, Not What’s Inside It
SQL Server: Fix a COALESCE Default Value That Gets Silently Truncated Because the Result Took the First Column’s Declared Length

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (609)
  • Average of all values in a column that are not zero in SQL (570)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (547)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (977)
  • Get the First and Last Word from a String or Sentence in SQL (858)
  • How to select distinct rows in a datatable in C# (854)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com