0

How can I write a WordPress meta query that gives me an array of users/posts that have a given amount of entries in a custom field that stores multiple values?

Something like SQL COUNT(), but for the number of entries in an ACF array field.

When writing a WP_Meta_Query and comparing with >, >=, etc. this doesn't seem to work for me, perhaps because the data is stored serialized.

So, basically, how can I perform a simple selection of records with a specific count inside their custom field value.

1 Answer 1

0

This isn't possible using WP_Query, you can't query the insides of structured data stored in a single post meta value.

At best you can use regular expressions, or do math/boolean comparisons, you can even do LIKE comparisons, but these are all string operations.

If you need to query structured data such as objects or arrays, then the data needs breaking apart into multiple separate post meta values.

At the end of the day, post meta values are just strings of text as far as the database is concerned. You can cast them to a different basic data type such as a date or number, but that's it.

1
  • 1
    Appreciate your clear explanations on the topic. 🙏
    – David Wolf
    Jan 24, 2022 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.