I have set up my posts with bookmarks where the user's meta will be updated with the post ID of the post they are bookmarking. They are stored in user meta key my-bookmarks as an array.

Now I am trying to query (using PHP) all of the users that have bookmarked a specific post, and list the counts by state. So 20 people from Wisconsin, 10 people from Florida, etc.

I am wondering if there is an easy way to fetch all of the possible values of the user meta key state from the users that have bookmarked that post, so I don't have to iterate all 50 states every single time. I would rather see that only users from 3 states have downloaded them, so let's just query the users from those 3 states.

Unless you have a better idea.

  • 1
    Storing bookmarks in an array in a single meta key is going to make this much harder and slower. You should store each bookmark as a separate row. Apr 29, 2021 at 4:15
  • I suppose that makes sense. Thanks, Jacob!
    – Michael
    Apr 29, 2021 at 14:25

1 Answer 1


Perhaps you could create a private utility taxonomy bookmarks, where each term would correspond to a user. When ever a user is created a new term would be added to the utility taxonomy. As the user bookmarks or removes one's bookmark from a post, your code would attach or detach the user equivalent term from the post.

The user ID could be saved as the term name and/or slug, which you could use to do "reverse lookups" e.g. turn the term name into a integer and query single user with it.

To find out who has bookmarked a post, you'd simply use get_the_terms($post_id, 'bookmarks'), turn the term names to an array of IDs, and query the users with WP_User_Query(array('include' => $user_ids)).

  • Thank you. That is an interesting approach. We have hundreds of users, though. Wouldn't having hundreds of terms in addition to the hundreds of users waste resources?
    – Michael
    Apr 29, 2021 at 14:23
  • @MrGandy I'm not much of a database expert, but yes, there would be some data duplication with this approach. But perhaps there could be some performance gains, as taxonomy queries are quite efficient in WP to my understanding, if you need to do something funky with the bookmarks data at later point. This is probably something that should be tested and measured, if possible. Apr 29, 2021 at 15:24
  • Thanks. I read up on this approach and others have said this is the best way performance-wise as well. I accepted your answer and will give it a try. :)
    – Michael
    Apr 29, 2021 at 16:05

Your Answer

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

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