Skip to content

Instantly share code, notes, and snippets.

@jreviews
Last active September 14, 2022 18:20
Show Gist options
  • Save jreviews/5a5dac82a38ca5e1635f0e3b1cb020cb to your computer and use it in GitHub Desktop.
Save jreviews/5a5dac82a38ca5e1635f0e3b1cb020cb to your computer and use it in GitHub Desktop.
PeepSo: Allow searching member profiles with multi-select fields

Within PeepSo fields it's only possible to enable single select lists as searchable. The following code uses PeepSo filters 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.

Create a peepso-search-member-profiles.php file in /wp-content/mu-plugins with the following code:

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

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 ( ! $post = get_post($fieldId)) {
            return;
        }

        $custom = new PeepSoFieldSelectSingle($post, 0);
        
        $custom->meta->method_form = '_render_form_select';        
        ?>
        <div class="ps-members__filter ps-members__filter--custom ps-js-filter-extended">
            <label class="ps-members__filter-label" style="display: block;">
                <?php echo $fields['peepso_user_field_'.$fieldId]->prop('title'); ?>
            </label>
            <?php $custom->render_input(); ?>
        </div>
        <?php
    }

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

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment