Listing entries assigned to a specific user

It's pretty easy to only display entries created by the currently logged-in user on GravityView by using our Advanced Filters. But, what if you want to display entries assigned to the currently logged-in user who did not create the entries?

In this short tutorial, we'll find out how to display a nice drop-down field on your form to list all your WordPress Users which we can then use to "assign" an entry to another user.

In our example, we'll create a contact form, that you can use to address a message to another member of your website.

Step 1: Preparing our Form

Here's a basic form:

Now we need to use a small script to automatically populate our drop-down field with our website users. 

This code requires two small modifications in order to work:

1) We need to specify our form ID at the beginning of the code (see image below). 

2) And we need to add the CSS class "user-emails" to our drop-down field, so that the script knows what field to use (see image below).

Related: Where do I add the code samples you share?

Now, if we preview our form, we'll see the drop-down automatically populated with our website usernames. The value of each option (user) is their WordPress user ID, perfect!

In order to use that field to compare against the currently logged-in user in GravityView, we'll need to store the selected choice of that drop-down field inside a hidden field on the form. To do that, we'll add a Hidden Field to our form and use this second script, which will copy the value of the drop-down choice into our Hidden Field.

Now, save your form and click on Preview to submit a test entry. And voilá:

Our script is working! You'll see in a few moments, why that was necessary when we configure the filter in our View.

Step 2: Creating our View

Let's create a View using the Table View layout, to make it simple for this tutorial:

As we can see in the image above, this View is unfiltered, we are seeing all messages. Now, let's implement a filter on this View using the Advanced Filter extension and the {user} Merge Tag to only display entries for the logged-in user.

The {user} Merge Tag will be used to output to the filter the ID of the currently logged-in user. This way we can compare it against our Hidden Field that contains the user ID where the message should be addressed to.

And here's the end result of the View with filters applied:

Since our user ID is currently 1, only messages addressed to that ID will be displayed. Fantastic!

We strongly recommend you read the article about the {user} Merge Tag, which is a powerful tool that can be used to retrieve any data from the currently logged-in user, even custom user meta can be used to compare against any form field.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.