• Resolved rufrage

    (@rufrage)


    Hey,

    great plugin, thanks a lot already!
    On my website, I give users multiple fields for job, skills, categories, etc.
    Your search plugin gives me the possibility to have users search for a word and list all the users, that have this word in any of their fields – excellent and just what I need!

    Now, for improved overview, I need them to be sorted by their job. As if this wasn’t enough already, I need a small headline above the first user of each job, mentioning the job name (no matter whether he is the only, or just the first listed with this job).
    I assume this can be achieved via a custom search results page, that you offer (is there even anything you did not think of already?). Unfortunately, I am new to code adjustments, but I would totally love to give it a shot and learn about it.

    So if you have any suggestions on how I can
    -sort by the job name by default
    -get a heading above the first person of every job
    I would be super happy and grateful!

    So long, have a good time and thanks for this awesome plugin!
    Rufrage

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter rufrage

    (@rufrage)

    I made some progress already – using

    <?php static $currentjob = "";
    		$memberjob = bp_get_member_profile_data( 'field=T?tigkeit' );
    		if(strcmp($currentjob, $memberjob) !== 0){
    			$currentjob = $memberjob;
    			echo "<div class=\"membersjob\">$currentjob</div>";
    		}
    		?>

    right below the
    <?php while ( bp_members() ) : bp_the_member(); ?> line, I can check if the member has the same job as his predecessor and only show the headline, if this is not the case.
    However, the users are not yet sorted by the value in their job field (“T?tigkeit” in my language), so it occurs to happen, that a certain headline appears more than once, simply because the predecessor had another job.

    How can I have the list sorted by the members’ jobs, so that everythings works out?
    Thanks in advance!

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hi Rufrage,

    Thank you for your kind words. The custom search results pages work this way:

    1. First, you build a custom members directory according to your needs. In your case, you would build a members directory that is ordered by Job Name, and with the sub-headings you need in the members list.

    2. Second, you select your new members directory as the Form Action (Results Directory) of your search form.

    Of course the hard part is the first one. I don’t have a first-hand experience in ordering the members directory by a profile field, so I run a few Google searches like:

    order buddypress members directory by profile field

    but wasn’t able to find a clear solution. I would assume this is not a simple task, but I could be missing something. Hopefully other readers can add their suggestions here.

    Thread Starter rufrage

    (@rufrage)

    Hello,

    thanks for your reply and your effort in finding a solution.
    I will look for a solution myself and post it in here, once I find one.

    Greetings!

    Plugin Author Andrea Tarantini

    (@dontdream)

    Great, please let me know of your progress!

    Thread Starter rufrage

    (@rufrage)

    Hey!

    I found code, where I put the following into functions.php

    function xfield_member_filter( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
    
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id, value FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " ORDER BY value" ;
      else
       return '';
      // var_dump($query); die;
      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 '';
       
    }
    
    function alphabetize_by_custom_field( $bp_user_query ) {
      ;
      if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) {
        $bp_user_query->uid_clauses['orderby'] = "ORDER BY FIELD(u.ID," . $bp_user_query->query_vars['include'] . ")";  
        $bp_user_query->uid_clauses['order'] = "";  
      }
    }
    add_action ( 'bp_pre_user_query', 'alphabetize_by_custom_field' );

    and this in the members-loop.php, where the usual request would stand:
    <?php if ( bp_has_members( xfield_member_filter( 'yourcustomfield ' ).'&type=alphabetical' ) ) : ?>

    It works, the fields are now gettings sorted by the custom field (which must be set instead of yourcustomfield in the last code-line).
    Unfortunately, it does no longer consider the search key terms for the query anylonger, thus showing the entire members dictionary.
    Do you have any clue on how to put your plugins’ search filter after this custom filter?

    Best regards,
    Rufrage

    • This reply was modified 8 years, 1 month ago by rufrage.
    Thread Starter rufrage

    (@rufrage)

    Edit: The regular BuddyPress search works fine and shows only the correct members (once I put in more that 3 characters – else it shows everybody?). This must be an issue conflicting the custom code and the plugin..

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hello Rufrage,

    I’ve been researching this topic for a few days now, and probably I’ll be able to release a new version of BP Profile Search with the ability to add new Order By options to member directories using profile fields.

    Your code looks good too, I would suggest these changes:

    function xfield_member_filter( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
    
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id, value FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id . " ORDER BY value" ;
      else
       return '';
      // var_dump($query); die;
      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 = implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';
       
    }
    
    function alphabetize_by_custom_field( $bp_user_query ) {
      ;
      if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) {
        $bp_user_query->uid_clauses['orderby'] = "ORDER BY FIELD(u.ID," . xfield_member_filter( 'yourcustomfield ' ) . ")";  
        $bp_user_query->uid_clauses['order'] = "";  
      }
    }
    add_action ( 'bp_pre_user_query', 'alphabetize_by_custom_field' );
    

    and in members-loop.php:

    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ). '&type=alphabetical' ) ) : ?>
    

    Please let me know how it works for you!

    Thread Starter rufrage

    (@rufrage)

    Unfortunately, now the search doesn’t output any members anymore. Instead, it says “No users found” (something like this, I have it with another language). Also, when I access the directory without any search queries, but via permalink, I can’t see any members..

    Thanks for you suggestion anyways – can you maybe think of another solution?
    Greetings,
    Rufrage

    Plugin Author Andrea Tarantini

    (@dontdream)

    My new BP Profile Search version is almost ready, if you wish I can send you a copy for testing. To create a new Members directory and order it using a profile field, you can simply create a new page and enter the shortcode [bps_directory]. Examples of usage are:

    [bps_directory order_by=field_47]
    
    [bps_directory order_by='field_47 desc, field_50']
    
    [bps_directory order_by='field_47 asc, field_14 both']
    

    Of course you can also customize the directory template, adding your sub-headings. If you are interested in trying it, please send your email address to [email protected]

    Thread Starter rufrage

    (@rufrage)

    Sure, I sent you an email!

    Plugin Author Andrea Tarantini

    (@dontdream)

    Hello rufrage,

    This feature (ability to sort a Members directory using a profile field) has been released with BP Profile Search 4.7.

    I’m closing this topic now. Thank you for testing the beta version!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Extend search results page by “sub-headings”’ is closed to new replies.