Support

Home Forums WP User Integration How can I get all event ID's for a WordPres user in the user Dashboard?

How can I get all event ID's for a WordPres user in the user Dashboard?

Posted: November 20, 2017 at 1:01 pm

Viewing 8 reply threads


kdaily

November 20, 2017 at 1:01 pm

I’m using the WP Users plugin, and all new event registrations create a new WP user. When they login, they are taken to their Dashboard where I show them custom content. What I need to do is to be able to show them some Event Espresso content, too.

I am using the event/page ID to pull some Advanced Custom Fields data for that user, specifically a file download link. But what I need to do is find all the event/page IDs for that one user while they are in their Dashboard.

I display the download link like this

echo get_field('downloads', 132);

i.e., for the event/page ID 132. That works now; but of course, the event/page ID needs to be dynamic for each user.

How can I get all event IDs for a user in their Dashboard? I need to loop through them in case there are more than one and then use

echo = get_field( 'downloads', $post_id );

with each $post_id being the event ID.

I’m looking at eea-wp-users-registrations-table.template.php, since that is used to display registrations on the Profile. But I can’t see how to copy and modify parts of that to get all event IDs by user, rather than registrations, in the Dashboard, and I get “Undefined variable: registrations” errors.

Thanks


kdaily

November 20, 2017 at 2:05 pm

I fond this after I posted my question: https://eventespresso.com/topic/ee4-how-to-get-list-of-events-user-sign-up-for/ The code snippet by Jon Ang is a good starting point for me. I tested the $user_id = get_current_user_id(); and it echoes, but $att_id = get_user_meta( $user_id, 'EE_Attendee_ID', true ); doesn’t. So does that mean I need to load EE data in a different way on an admin page like the Dashboard?


Tony

  • Support Staff

November 20, 2017 at 2:28 pm

$att_id = get_user_meta( $user_id, 'EE_Attendee_ID', true );

So $att_id is null?

So does that mean I need to load EE data in a different way on an admin page like the Dashboard?

No.

Have you completed a registration using the user account you are using above? It’s pulling the current user_id and then checking if it has an EE_Attendee_ID assigned to it, so if you haven’t completed a registration on that user account using details that match the user account you won’t have that value.

This is how I would pull an array of Event IDs that a user has registrations for:


//Pull in the current user object for an example.
$user = wp_get_current_user();

//Grab the EE_Attendee_ID for that user.
$att_id = get_user_option( 'EE_Attendee_ID', $user->ID );

//Check we actually have an EE_Attendee_ID.
if ( empty( $att_id ) ) {
	return; //bail, no attached attendee_id.
}

//grab the contact using the above ID
$contact = EEM_Attendee::instance()->get_one_by_ID( $att_id );

//if no contact then bail
if ( ! $contact instanceof EE_Attendee ) {
	return;
}

//Pull all registrations related to this contact.
$registrations = $contact->get_many_related( 'Registration' );

//Loop through all registrations and pull the event_ID for each on into an array.
foreach( $registrations as $registration) {
	if( $registration instanceof EE_Registration ) {
		$event_IDs[] = $registration->event_ID();
	}
}

//We only need to the event ID for each event once, so run the array through array_unique()
$event_IDs = array_unique($event_IDs);

//Do whatever with the event ID's
var_dump($event_IDs);


kdaily

November 20, 2017 at 3:06 pm

Ah, thanks! Starting to make sense. I did have one registration, but it wasn’t showing up with my first code snip. With your function, it did in var_dump, as array(1) { [0]=> int(132) . And with two registrations, I get array(2) { [0]=> int(132) [2]=> int(46)

With one registration, echo = get_field( 'downloads', $post_id ); echoed the data. But with two or more: how do we give each its own line?


Tony

  • Support Staff

November 20, 2017 at 4:11 pm

You need to loop through the array of event ID’s and pull the meta field for each, something like:

foreach( $event_IDs as $event_ID ) {
    echo = get_field( 'downloads', $event_ID );
}


kdaily

November 21, 2017 at 11:11 am

Thanks! THis works:

foreach( $event_IDs as $event_ID ) {
    echo get_field( 'downloads', $event_ID ); echo '<br />';
}

Problem now is that while the registration list in the profile shows canceled registrations, my loop shows downloads. So I need to only show users who are approved for event(s). Is there a variable for that?


Tony

  • Support Staff

November 21, 2017 at 12:43 pm

Have you thought about using the my events section of the WP user add-on?

There’s a front-end facing version and one within the admin, look at the users profile and you’ll find it there.

As for pulling only Approved registrations you can use:

$registrations = $contact->get_many_related( 'Registration', array( array( 'STS_ID' => EEM_Registration::status_id_approved ) ) );

In place of the original call to just pull all registrations.


kdaily

November 22, 2017 at 11:13 am

I did look in those files but couldn’t figure it out. Replacing $registrations = $contact->get_many_related( 'Registration' ); with your new snip gives me Undefined variable: event_IDs, invalid argument supplied for foreach() and array_unique() errors.


kdaily

November 22, 2017 at 7:44 pm

Sorry, that does work. My mistake. Thanks!

Viewing 8 reply threads

The support post ‘How can I get all event ID's for a WordPres user in the user Dashboard?’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso