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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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)).
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0