Development, PeepSo

Updated on

Making PeepSo multiple select profile fields searchable

Alejandro Schmeichler

In this post you will learn how to make PeepSo multiple select profile fields searchable. At the time of writing, PeepSo doesn't offer search functionality for multiple select profile fields, but we can overcome this by taking advantage of some of the WordPress hooks PeepSo exposes in their code.

A large number of our JReviews WordPress clients also use PeepSo, so that is how this post came about. If you are interested in adding a directory with best in class review functionality to your website that offers great integration with PeepSo you can learn more in our post Building a vibrant social community site with PeepSo and WordPress.

So let's get into it. There are two things we need to do to make this work:

  • Make PeepSo believe the multiple select fields are actually single select fields, so it will include them by default in the profile fields search form.
  • Modify the search profile search query to include the selected values from the multiple select fields

There's a limitation that it will only be possible to filter results by selecting a single option from the field, but that is already an improvement over the default functionality.

Create the PeepSo search member profiles plugin

First create a peepso-search-member-profiles.php file in /wp-content/mu-plugins. If you are not familiar with mu-plugins, these are WordPress Must Use Plugins, which are always loaded on your site. You can optionally add the code to your theme's functions.php file, but then it won't be processed if you switch themes. Optionally you can also create a separate plugin.

In the PeepSo Fields Manager, find the IDs for the multiple select fields that you want to include in the member filters and add them to them to the $profile_search_fields array as shown below where we have 100 and 101 as the field IDs. These will be different numbers for you and you can add as many as you want.

<?php

/**
 * Find the field IDs under PeepSo Manage Fields
 * Add the field IDs of multiple select fields you want to show for member search
 */
$profile_search_fields = [
    100,
    101,
];

Trick PeepSo into believing multiple select fields are single select

Next we'll use the PeepSo peepso_action_render_member_search_fields action hook that allows including additional content after the existing PeepSo member filters. We'll output the filters for the field IDs you added to the array above. The trick here is to make PeepSo believe these are single select fields so it will attach the search functionality to them.


add_action('peepso_action_render_member_search_fields', function() use ($profile_search_fields) 
{
    $PeepSoUser = PeepSoUser::get_instance(0);
    
    $fields = $PeepSoUser->profile_fields->get_fields();
    
    echo '<div style="width: 100%; display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));">';

    foreach ($profile_search_fields as $fieldId) {

        if ( ! $field = get_post($fieldId)) {
            return;
        }

        $custom = new PeepSoFieldSelectSingle($field, 0);
        
        $custom->meta->method_form = '_render_form_select';        

        echo '<div class="ps-members__filter ps-members__filter--custom ps-js-filter-extended">';
        echo '  <label class="ps-members__filter-label" style="display: block;">';
        echo        $fields['peepso_user_field_'.$fieldId]->prop('title');
        echo '  </label>';
        echo    $custom->render_input();
        echo '</div>';
    }    

    echo '</div>';    
}, 100);

At this point you should be able to see the chosen fields under member filters.

Intercept the members search database query so it will also search multiple select values

The final step is to modify the database query PeepSo performs to filter the members list to to also process the data from the newly added filters. To do that we the PeepSo WordPress filter hook peepso_user_search_args:


add_filter('peepso_user_search_args', function($args) use ($profile_search_fields) {

    $args['meta_query'] = [
        'relation' => 'AND',
    ];

    foreach ($profile_search_fields as $fieldId) {
        $selected = $_GET['profile_field_'.$fieldId] ?? null;
        
        if ($selected) {
            $args['meta_query'][] = [ 
                'key' => 'peepso_user_field_'.$fieldId,
                'value' => $selected,
                'compare' => 'LIKE'
            ];
        }
    }

    return $args;
}, 100, 1);

We use the LIKE search, rather than a straight equal search because of the way the multiple selected options are stored for each member.

And that's it. You should now be able to filter members using multiple select fields in PeepSo. Any changes to styling, layout and functionality are left as an exercise to the reader 😉