0

I'm still a WordPress rookie and I'm trying to see what is the best approach to handle this. I have a website with about 10,000 users. I also have a Events post type and on each event detail page, users will be able to click "I'm interested in this event" should assign this user to an event. How should I handle this? How should I implement assigning multiple users to a custom post type (there will be an admin section to view Event's users).

Thanks,

1
  • why not just use events-calendar? it does all this.
    – rudtek
    Commented Nov 15, 2016 at 17:19

1 Answer 1

0

You can create for each Event a custom post meta (example event_users) where you will save the users ID's that clicks the "I'm interested in this event"

Something like:

$event_users = get_post_meta($event_id, 'event_users', true); // Get exisitig event users $event_users[$user_id] = 1; // Add user ID as a KEY, a simple way to make sure you only add it once add_post_meta($event_id, 'event_users', $event_users, true) or update_post_meta($event_id, 'event_users', $event_users); // Add/update event users

Then when you wnat to use this data to get all users for one event is easy just use the $event_users = get_post_meta($event_id, 'event_users', true); Getting all events for a user is a bit more tricky. You can use the WP_Query with meta_query like:

[ `key` => 'event_users', `value` => '%:'. $user_id .';%', 'compare': 'LIKE' ]

PS: Array value are serialized on saved, but to be honest I do not like to much serialized values (is just a personal thing), I prefer to convert the values to JSON (json_encode on save, and json_decode when you extract data from DB)

In case you will use json data format meta query will look something like:

[ `key` => 'event_users', `value` => '%"'. $user_id .'"%', 'compare': 'LIKE' ]

Check this links for more info: - https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters - https://codex.wordpress.org/Function_Reference/add_post_meta

A dedicated plugin for events: https://wordpress.org/plugins/the-events-calendar/

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.