0

I’ve created a Custom Post Type to manage Events and now, i would like the users of my site (only registered ones) to be able to register to any events created. On the events page, they would just click a button and they would be associated with the event (no need for tickets, payment,…). I have no clue on where to look at to implement that (taxonomies, meta, … ?) and how this will be stored (on user entity, on event custom post type entity?) I need help !

Thanks

1 Answer 1

0

I suggest you to save it on user entity. Use update_user_meta() and stock it as an array. In this case if you delete an user, all associates info will be delete too.

<?php
$user_id = get_current_user_id();
$user_events = get_user_meta($user_id, 'user_events', true);
$user_events[$event_id] = $event_id; //you can also stock more infos, i use event_id as key to don't have duplicate event by user
update_user_meta($user_id, 'user_events', $user_events);
?>
4
  • Thanks, Jerome ! This is helpful. Storing array's in post-meta or user_meta seems to be the right way to do. Now, I have to chose the right one. Your remark regarding deleted users makes sense. I di not think about it. Now, if I save the event_id arrayin users post_meta, to display the list of users for a specific event, I will have to query users data and find out who is associated with that event_id. Does not sound practical or performant. The same will happen the other way around if I want to display events connected to a specific user. Commented Mar 10, 2017 at 14:26
  • Maybe the solution will be to duplicate the data in both user_meta and post_meta. But duplicating data is something I try at the maximum Commented Mar 10, 2017 at 14:30
  • So, you can create a custom database table for relationships. And use add_action( 'delete_user', 'your_function' ) to clean useless data. Commented Mar 27, 2017 at 8:53
  • thanks. After thinking the design, this is indeed the approach I will take. Thanks again Commented Mar 27, 2017 at 8:58

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.