Home Forums General Issues Get count of same values in an array Reply To: Get count of same values in an array

  • You WP query won’t work the way you’re doing it, a meta value can’t an array as far as I know.

    There really isn’t what I would consider a “Good” solution to this problem. WP really doesn’t offer a way to get the number of posts with a specific meta_value using WP_Query

    These are the choices I can think of.

    1) Get all the posts and loop through them twice, the first to see how many are in each “Group” and the second to output them. You can group them by using a new feature of WP_Query added in 4.2 and outlined here.

    I think this is probably the best choice because you’re only doing one query. At the end of the first loop you can use rewind_posts()

    2) Do 3 separate queries, one for each group, with a meta query for each groups, loop through the posts and store the post into a separate array for each group. Then you know know many are in each array and you can loop over each array to display. This is not as good a solution because you’re doing 3 queries.

    3) Probably the best choice, but a bit more complicated coding wise. Skip WP_Query and use the wpdb class directly. While it would probably be the best choice I don’t generally want to work that hard if I can accomplish what I want without it, as in my 1st suggestion.