Search code examples
phphtmlwordpresssearchbuddypress

BP search_term multiple fields


trying to search for members in my blog using multiple fields.

<?php if ( bp_has_members( "search_terms=Red Box" ) AND bp_has_members( "search_terms=Blue Box" ))  : ?>
<?php while ( bp_members() ) : bp_the_member(); ?>

…html here

<?php endwhile; ?>
<?php endif; ?>

However, if I use this code the result shows me just the 'Blue Box' Members and no both.

Any Advise?


Solution

  • From the BuddyPress documentation:

    To search on multiple terms, put a space between each term. The search is based on ‘AND’, not ‘OR’. So only members who have selected / entered both ‘poodles’ and ‘carrots’ will be returned.

    <?php if ( bp_has_members( 'search_terms=poodles carrots' ) ) : ?>
    

    This apparently does not work if the search terms contain spaces.

    See the documentation for The Members Loop, specifically the my_custom_ids() example. You can use a similar function to pass in query terms, run a SQL statement to only get members those terms apply to, then return a string that looks like include=1,2,5,6,7 to fetch only those user IDs.