While you can do this with WP_Query
, I’d do this the way you are trying to avoid– save a list in a separate key. The “pros” strongly outweigh the “cons” in my mind.
LIKE
queries with wildcards, especially with leading wildcards,
are not efficient– aka, “slow”- and a
LIKE
query on serialized data is going to be prone to error - You will likely be running this query on display, which means that users see a delay
Whereas…
- The “space” needed to store another value in, say, the options table
is negligible - And the query to retrieve the value is as about as efficient as a
query can be - The work is pushed almost entirely to the backend, when the data is
saved, so that any delay is seen by the administrators/authors of
the site and not by the ordinary users.
You can add and retrieve the data with the core functions update_option
and get_option
, the latter of which has caching mechanisms built in.