ACF: How to query for a given value count of an array like field? (e.g.: How many rows has a `flexible_content` field?)

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.

Topic advanced-custom-fields meta-query count wp-query custom-field Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.