Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter Profile Fields


  • Kasper Kloster
    Participant

    @klame

    Hi,
    I’m struckling a bit over here..
    I’m trying to let visitors filter members by profile fields.

    At the registrion form users can choose genre from checkbox:
    Rock, Hip-hop, Jazz, Indie etc. – User can choose as many as they want.

    At the members-loop / buddypress-index, I want visitors to be able to filter from these values.

    So if they click on “Jazz” and “Rock”
    All users that have the chosen values, will appear.

    But I can’t get “Genres” / Profile-fields out..

    Can somebody help me in the right direction?
    Thanks.

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

  • danbp
    Moderator

    @danbp

    This is possible as built-in feature from within a profile. Each field value is clickable by default (in fact the 5 first words). When clicked, you get a list of all members who entered the same value.

    To list the member directory by xprofile field values, you have to code it or modify the directory template. Read from here:

    Members Loop


    Kasper Kloster
    Participant

    @klame

    Thanks for your reply!
    Yes, I have seen that.. But I can’t get it to work.
    I’m supposed to correct the function ($field_name and $field_value) right?

    So:

    function my_custom_ids( $genre, $field_value = 'Rock' ) {
      
      if ( empty( $genre ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $genre ); 
     
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
      else
       return '';
      
      if ( $field_value != '' ) 
        $query .= " AND value LIKE '%" . $field_value . "%'";
          /* 
          LIKE is slow. If you're sure the value has not been serialized, you can do this:
          $query .= " AND value = '" . $field_value . "'";
          */
      
      $custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }

    ^
    That script is in my function.php
    I have also tried not to change $field_name and tried all case senstive options (I think).

    In members directory I have included this:
    <a href="<?php if ( bp_has_members( my_custom_ids( 'Genre', 'Rock' ) ) ) ; ?>">Rock</a>

    So that should look for what is in ‘Genre’ called ‘Rock’, and give you those when you click on the link.. What have I misunderstood?


    Mark
    Participant

    @markob17

    Hi @klame,

    I am a newbie at all this too. I discovered that the function you pulled off Buddypress.org doesn’t have to be modified to work. You have to simply copy and paste it in your functions.php file as/is without changing a thing. What matters and needs to be modified is the php to render the members loop on your page (e.g., <?php if ( bp_has_members( my_custom_ids( 'location', 'san francisco' ) ) ) : ?>). Adding href tags around it I don’t believe is the right idea. That code goes in your members loop where you want the members to be displayed matching that criteria. In my example I am showing only users in San Francisco. I’m pretty sure you can display the members loop anywhere you want but I haven’t tried that yet. I decided to create a page template and I simply copied the code out of my theme’s buddypress members-loop.php file and pasted it into the template file. You could probably even use the default WordPress members-loop.php file as your base, though if you have a theme you’re better off getting it out of there. I hope this helps. Good luck!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filter Profile Fields’ is closed to new replies.
Skip to toolbar