This code recipe demonstrates how to allow users to filter results shown by the Member Directory and Profile Pages Add On by adding a widget to the member directory’s sidebar. This feature allows users to search through your member directory more effectively while also improving their experience on your site.

Allow Users to Filter Results in your Member Directory

About the Code Recipe

The Member Directory and Profile Pages Add On provides a shortcode to display your site’s members and information about those members in a variety of different layouts. This functionality is typically used to give users a means of finding members on the site. As your site grows, it becomes increasingly difficult to find the particular member information that your users are searching for.

One way to combat this issue is by providing filterable values that users can select to narrow their search and allowing them to more easily find the member that they are looking for. This code recipe gives you the functionality to do just that.


Full Directory (no filters selected)

Allow Users to Filter Results in your Member Directory

Filtered Directory View

Filtered View of PMPro Member Directory

Setting Up Your Member Directory Filter Widget

  1. You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.
  2. Add your filterable fields to the widget by updating the widget() function (specifically lines 36-90 at the time of writing).
  3. Update the SQL query based on user input into the fields you created in the my_pmpro_directory_widget_filter_sql_parts() function.
  4. Insert the widget into a sidebar shown on your Member Directory page via the WordPress Appearance > Widgets screen. This widget will not show any content on pages without the member directory shortcode.
  5. Test to make sure that your filters are working as expected.

The Code Recipes

This code recipe requires a PMPro Plus Account or higher.

View Membership Options

David Parker

This code recipe was written by our very own David Parker.

Comments (10)

Hello,
This looks like the code I’ve been looking for to improve my team’s site, but I have a few things I would like to confirm. Would I be able to filter by location as in relative distance from user? Also, is it possible to filter by type of business? For example woman owned, veteran owned, etc. Finally will this work well with other PMP add-ons, or will I need to rework our existing site?

Hi David,

We’ve loaded the recipe with a few of our own fields, and it’s working well, so thank you. Can you advise how to change the presentation of a field from checkbox to select – we’ve changed the words in the obvious two places, but the result was an epic fail.

Many thanks.
Cheers.

Hello,

I’ve managed to make the widget visible on the website but it seems that it never manages to write any custom SQL query. I’ve tried to do a very simple query just manually choosing a single user with a given id but it never executes.

It seems the same query is executed all the time regardless of any modifications to the $sql_parts array, you can verify here: https://pastebin.com/FGaQ462R

In order to determine why the SQL query is not being updated, it would be helpful to see the full customizations code that you are running on your site.

If you would like further help debugging this issue, can you please open a new ticket from our Member Support page and include a link to a Gist containing your custom code in that ticket?

This is maybe an obvious question, but I’m looking for the table/column names in MySQL for the fields I want to filter by so that I can modify the SQL query. Where should I start?

The table and columns that you should use in your query depend on what information you are trying to filter your directory by. If you are trying to filter by a field added using PMPro Register Helper, you will want to query using the usermeta table as shown in the code recipe.

If there is a non-Register Helper value that you would like to filter by, please open a ticket from our Member Support page and we would be happy to give more specific guidance there.

Hi David,
Thank you SOOOO MUCH for this code. It was exactly what I have been looking for for some time. It works just perfect.

Can you please extend it with a reset button as well?

Hi there, happy to hear that this code is working on your site! There are two main ways that I would consider creating a reset button:
1. Add a button that links to your member directory page with no parameters being passed in through the URL. Since the filters in this code recipe are passed in via the URL, removing those parameters will remove all filters and “reset” the page.
2. Add a button that, when clicked, runs a Javascript function that goes through all the fields you’ve created and clears them.

If you have additional questions on how to implement this behavior on your site, we would be happy to help if you open a ticket on our Member Support page: https://www.paidmembershipspro.com/new-topic/

Leave a Reply

For faster support related to issues on your specific site please open a ticket in our members support area.

Your email address will not be published. Required fields are marked *