Free

API Design · E-commerce · Data Integrity

Two filters selected, results from either one returned. Some APIs treat multiple filter parameters as OR, not AND. How to enforce conjunction locally without losing correctness or pagination.
November 27, 2026·8 min read·By Abhay Chauhan (Full Stack Developer)

When the API's AND Is an OR: Manual Filtering at the Boundary

#API Design#E-commerce#Data Integrity#Filtering#Best Practices
The short answer: Some APIs interpret multiple filter parameters as OR rather than AND. If your product requires conjunction, do not trust the parameter combination — fetch a superset and enforce every filter locally. Resolve each selected filter to a stable ID, apply all predicates with AND semantics, and make sure your fan-out reads every page before filtering.

A user selected a brand and a category. The results included products from that brand in other categories and products in that category from other brands. The API had quietly applied OR where the UI promised AND.

This is a contract-semantics bug, and it is common enough to be worth a general pattern.


1. Verify the semantics

Do not assume. Construct a test where the intersection of two filters is strictly smaller than either set, then inspect the result:

  • If you get the intersection, the API uses AND.
  • If you get the union, it uses OR.

Most filter UIs want AND: "red AND size M", not "red OR size M". Once you know the API disagrees, you have two choices.


2. Fetch a superset, filter locally

Pass the single most selective filter to the API to narrow the fetch, then enforce the rest in your own code:

ts
const products = allFetchedPages.filter(p =>
  p.categories.some(c => c.id === selectedCategoryId) &&
  p.brands?.some(b => b.id === selectedBrandId)
);

Now conjunction is guaranteed by your code, independent of the API's interpretation.


3. Resolve stable identifiers

Filter on numeric term IDs, not slugs or translated names. Slugs vary by locale; IDs do not. If the UI carries slugs, resolve each one to its term ID before filtering — and reject the query if any slug cannot be resolved, because an unresolved filter can never be satisfied.


4. Read every page before filtering

This is the part that is easy to get wrong. If you filter only the first page, matching products on later pages vanish. The fan-out must read the entire superset first, with bounded concurrency and field projection to control cost:

code
fetch all pages → flatten → filter with AND → paginate locally

Local pagination after filtering ensures page numbers refer to the filtered set, not the raw one.


5. Make the workaround observable

Log when the API's reported count disagrees with your filtered count:

ts
if (filtered.length < apiTotal) {
  console.warn(`[filters] API returned ${apiTotal}, AND-filter kept ${filtered.length}`);
}

A workaround that silently becomes unnecessary — or silently becomes insufficient — is a maintenance trap. Logs tell you when the upstream behavior changes.


6. Defense in depth

ConcernTrust API combinationLocal AND
SemanticsVendor-dependentGuaranteed
Multiple filtersUnion leaksIntersection only
Later pagesLostIncluded via fan-out
VisibilityNoneLogged discrepancy

How to enforce AND reliably

  1. Test the API's actual multi-filter semantics.
  2. Resolve every filter to a stable identifier.
  3. Fetch a superset using the most selective filter.
  4. Apply all predicates locally with AND.
  5. Read all pages before filtering; log count mismatches.

Frequently asked questions

Is local filtering slow? It fetches more data, but field projection and bounded fan-out keep it acceptable. Correctness first.

Can I cache the filtered result? Yes, keyed by the full filter combination — but invalidate it when the catalog changes.

What if two filters have no intersection? Return an empty result with a clear message, not a fallback to one filter.

Will this break pagination totals? Only if you use the API's total. Use the locally filtered length for pagination.


Key takeaways

  • Verify AND vs OR semantics explicitly.
  • Fetch a superset and filter locally when needed.
  • Resolve filters to stable IDs.
  • Read all pages before filtering.
  • Log discrepancies so the workaround stays visible.

Summary

When an API's semantics do not match your product's promise, trust your own code. A superset fetch and an explicit AND predicate turn a silent wrong answer into a correct one.


References

  • WooCommerce REST API documentation.
  • MDN Web Docs — Array.prototype.filter.
  • Google Search Central — faceted navigation guidance.

Got something you're building?