Development PeepSo

Updated on

Making PeepSo multiple select profile fields searchable

Alejandro Schmeichler

While this isn't a JReviews-specific blog post, if you are using PeepSo for WordPress I am sure you'll find it useful. Recently, a JReviews WordPress client found that it was not possible to make PeepSo multiple select profile fields searchable. In this post you'll learn how to implement a simple workaround to improve the current functionality and make multiple select fields available for filtering in PeepSo.

The following code takes advantage of PeepSo's Wordpress hooks to allow you to include multi-select fields in the members search area by converting them to single select and including the selected value in the database query that retrieves the member results. 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.

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,
];

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. 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 😉