Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filtering by Xprofile fields


  • puzzld1
    Participant

    @puzzld1

    I have a members loop where I am trying to filter by an xprofile field. The codex shows the example that will return members that filled out the xprofile field ‘dogs’ and selected the value ‘poodles’:

    <?php if ( bp_has_members( my_custom_ids( 'dogs', 'poodles' ) ) ) : ?>

    How would I write something that would give me members that filled out the xprofile field ‘dogs’ and selected ‘poodles’ OR ‘terriers’?

Viewing 3 replies - 1 through 3 (of 3 total)

  • puzzld1
    Participant

    @puzzld1

    Also, in a separate members loop, how would I write something that would give me all members that filled out the xprofile field ‘dogs’ with any value AND filled out the xprofile field ‘shirt size’ with the value ‘small’?


    zeeshanmba2014
    Participant

    @zeeshanmba2014

    To filter members who have filled out the xprofile field ‘dogs’ and selected either ‘poodles’ or ‘terriers,’ you can use the bp_has_members loop with a custom function to achieve this. Here’s how you can modify the code:

    php
    Copy code
    <?php
    // Create a custom function to filter members by xprofile field values
    function my_custom_ids($field_name, $values) {
    global $wpdb;

    // Prepare an array of values to match
    if (!is_array($values)) {
    $values = array($values);
    }

    // Generate the SQL query to retrieve user IDs with the specified field and values
    $sql = $wpdb->prepare(”
    SELECT user_id
    FROM {$wpdb->prefix}bp_xprofile_data
    WHERE field_name = %s
    AND value IN (‘” . implode(“‘,'”, $values) . “‘)
    “, $field_name);

    // Fetch the matching user IDs
    $user_ids = $wpdb->get_col($sql);

    // Return the user IDs
    return $user_ids;
    }

    // Define an array of values for ‘dogs’ xprofile field
    $dogs_values = array(‘poodles’, ‘terriers’);

    // Check if there are members matching the criteria
    if (bp_has_members(array(‘include’ => my_custom_ids(‘dogs’, $dogs_values)))) :
    ?>

    <!– Your member loop code here –>

    <?php
    endif; // End bp_has_members loop
    ?>
    In this modified code:

    We create a custom function my_custom_ids to query the database for user IDs based on the specified xprofile field name and an array of values.

    We define an array $dogs_values containing the values ‘poodles’ and ‘terriers’ that you want to search for.

    In the bp_has_members loop, we use the ‘include’ parameter to pass the array of user IDs returned by the my_custom_ids function. This ensures that the loop only includes members who have selected either ‘poodles’ or ‘terriers’ for the ‘dogs’ xprofile field.

    Make sure to replace <!– Your member loop code here –> with the code to display the members that match your criteria. This code should give you members who have filled out the xprofile field ‘dogs’ and selected either ‘poodles’ or ‘terriers.’


    puzzld1
    Participant

    @puzzld1

    Thanks for your response! It is giving me the members who have filled out ‘dogs’ with ‘poodles’ or ‘terriers’, but it then continues on with a random list of members who have filled out ‘dogs’ with something else or not at all. Here is exactly what I have, members loop included:

    <?php
    // Create a custom function to filter members by xprofile field values
    function my_custom_ids($field_name, $values) {
        global $wpdb;
    
        // Prepare an array of values to match
        if (!is_array($values)) {
            $values = array($values);
        }
    
        // Generate the SQL query to retrieve user IDs with the specified field and values
        $sql = $wpdb->prepare("
        SELECT user_id
        FROM {$wpdb->prefix}bp_xprofile_data
        WHERE field_name = %s
        AND value IN ('" . implode("','", $values) . "')", $field_name);
    
        // Fetch the matching user IDs
        $user_ids = $wpdb->get_col($sql);
    
        // Return the user IDs
        return $user_ids;
    }
    
    // Define an array of values for ‘dogs’ xprofile field
    $dogs_values = array('poodles', 'terriers');
    
    // Check if there are members matching the criteria
        if (bp_has_members(array('include' => my_custom_ids('dogs', $dogs_values)))) :
    ?>
    
    // Members loop here
    <?php bp_nouveau_pagination( 'top' ); ?>
    <ul id="members-list" class="<?php bp_nouveau_loop_classes(); ?>">
    <?php while ( bp_members() ) : bp_the_member(); ?>
    	<li <?php bp_member_class( array( 'item-entry' ) ); ?> data-bp-item-id="<?php bp_member_user_id(); ?>" data-bp-item-component="members">
    		<div class="list-wrap">
    			<div class="item-avatar">
    				<a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar( bp_nouveau_avatar_args() ); ?></a>
    			</div>
    			<div class="item">
    				<div class="item-block">
    					<h2 class="list-title member-name">
    						<a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
    					</h2>
    					<?php bp_nouveau_members_loop_buttons(array('container' => 'ul','button_element' => 'button',)); ?>
    				</div>
                       <?php do_action( 'bp_after_members_loop' ); ?>
    			</div><!-- // .item -->
    		</div>
    	</li>
    <?php endwhile; ?>
    </ul>
    <?php bp_nouveau_pagination( 'bottom' ); ?>
    <?php else : bp_nouveau_user_feedback( 'members-loop-none' ); endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar