Forum Replies Created
-
@hnla – yes it’s more complicated than it first seems. What I have works, just doubt it is the most efficient. Hopefully someone that has had to deal with this has a better solution that mine.
@hnla – Hi Hugo. Thanks for answering. I think my question may not be clear and some terms are getting confused with others. The tags you describe above are for the members directory aren’t they?
For example, this would be located at example.com/members/. This is not the page I need to check for. I need to check for the main page of a members profile, e.g. example.com/members/exampleuser/. All of the other pages seem to have a tag to check for them, except this page.
Also, this needs to be for the main page of any profile, not just the logged in user, e.g. bp_is_home.
Ok, I have sort of figured this out myself, however it feels a little ‘hacky’ as I would have thought BP has a built in function to determine if it is the ‘main page’ of a users profile.
This is what I added to home.php, where the if else statements for the different template parts are:
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $userID = bp_displayed_user_id(); $userURL = bp_core_get_username($userID); $testURL = $_SERVER['SERVER_NAME'].'/members/'. $userURL . '/'; //echo($host); /*Just to check*/ //echo($testURL); /*Just to check*/ if ( $host == $testURL ) : bp_get_template_part( 'members/single/profile/profile-loop' );This appears to accurately check if you are on the ‘main’ user page and then displays the profile template.
If there is a better way then please do let me know.
In reply to: Adding new 'Order By' on members loop@shanebp – Thank you this works really well. Weird, bug though. If you use the text search box, this breaks the pagination (without me editing any of your code). I’m guessing it has something to do with the URL parameters?
?s=examplesearch&members_search_submit=SearchThis doesn’t break the pagination on the first page, but on page 2 and then when clicking back to page 1. The label displaying the number of members etc. is wrong and also the number of results per page is then wrong as it displays all results.
Interestingly, is it not possible to pass additional parameters either via members-loop.php? e.g.
if ( bp_has_members( bp_ajax_querystring( 'members' ).'member_type=expert' ) ) :or via your code in bp-custom.php? I’m thinking I’m getting something mixed up when trying to add these additional parameters. It’s because I wanted to make use of the new ‘member_type’ in the latest version of BuddyPress but using a ‘member_type=exampletype’ doesn’t seem to work.
In reply to: Adding new 'Order By' on members loop@shanebp – That is cleaner and works, but pagination still doesn’t. Is it not possible to pass in the BP_User_Query parameters such as per_page in this format:
$object->query_vars['per_page'] = 10;Tried this, but it seems to not work.
Do I need to use bp_pre_user_query_construct instead? I’m guessing I need to create a new function passing in these parameters?
Edit: I created a new function passing in this parameter:
$query->query_vars['per_page'] = 10;I then add a new action for bp_pre_user_query_construct. This does indeed limit the first page’s results to 10; however it doesn’t display pagination to go to page 2 and it shows “1 – 11 of 11 Members” instead of “1 – 10 of 11 Members”.
In reply to: Adding new 'Order By' on members loop@shanebp So I have sort of made progress. I found this post by @eberger3. He seemed to be trying to do something similar to me. With his code I have now managed to order the results by price, which is great, but I am also experiencing the same issues he had:
- The pagination doesn’t work
- The member count shows ‘Viewing 1 member’ when there are many more
- The “search members” box doesn’t work. It returns the entire directory
Is this because I am using bp_user_query and not bp_ajax_querystring?
<?php function ch_member_order_options() { ?> <option value="price"><?php _e( 'Price', 'buddypress' ); ?></option> <?php } add_action( 'bp_members_directory_order_options', 'ch_member_order_options' ); add_action( 'bp_pre_user_query', 'sort_by_price' ); function sort_by_price( $bp_user_query ) { // Only run this if one of our custom options is selected if ( in_array( $bp_user_query->query_vars['type'], array( 'price') ) ) { global $wpdb; $field_id = 5; $user_query = $wpdb->get_col("SELECT object_id FROM " . $wpdb->prefix . "term_relationships WHERE term_taxonomy_id = " . $field_id); $users_str = ''; foreach ( $user_query as $user ) { $users_str .= $user . ','; } $users_str = rtrim($users_str, ","); // Adjust SELECT $bp_user_query->uid_clauses['select'] = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data"; // Adjust WHERE $bp_user_query->uid_clauses['where'] = "WHERE user_id in (" . $users_str . ") AND field_id=18"; // Adjust ORDER BY $bp_user_query->uid_clauses['orderby'] = "ORDER BY CAST(value as UNSIGNED) ASC"; $bp_user_query->uid_clauses['per_page'] = 10; } } ?>Edit: I can see this thread is in Installation category. This was a mistake. I should have but it into ‘How To’ or ‘Creating and Extending’ I can’t change this though.
In reply to: Members Page custom URL with select box valueHi @aaronkin
Did you actually manage to get your custom ‘sorts’ to actually sort the data or does it just filter it? I have managed to add a new option to ‘Order By’ which I can use to filter results, but it won’t sort the data by a profile field. It will only accept the current types, i.e. ‘alphabetical’ ‘last active’ etc.
In reply to: Adding new 'Order By' on members loop@shanebp I tried
$ch_querystring['user_ids'] = $users_str;but I think that only works for bp_user_query();. I saw the example before about how to sort by last name by @danbp and I think I should be able to modify this, but I can’t seem to get it to work:public function alphabetize_by_last_name( $bp_user_query ) { if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) $bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)"; } add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );Also, another weird thing is when I change ‘Order By’ to ‘Price’, all of the results show up, but they ignore what is in the search box. I added some code to take the search parameter in the URL and if you reload the page with the search parameter and ‘Price’ as ‘Order By’ then it filters the results as expected, both with ‘Order By’ and with the search term.
If you then change the ‘Order By’ to another option such as ‘Alphabetical’ and then back to ‘Price’, then the AJAX results don’t take into account the search terms. Not sure if I’ve just made a stupid mistake.
function ch_member_order_options() { ?> <option value="price"><?php _e( 'Price', 'buddypress' ); ?></option> <?php } add_action( 'bp_members_directory_order_options', 'ch_member_order_options' ); // filter ajax members by contributing function ch_filter_ajax_querystring( $querystring = '', $object = '' ) { if( $object != 'members' ) return $querystring; $defaults = array( 'type' => 'active', 'action' => 'active', 'scope' => 'all', 'page' => 1, 'user_id' => 0, 'search_terms' => '', 'exclude' => false, ); $ch_querystring = wp_parse_args( $querystring, $defaults ); if( $ch_querystring['type'] == 'price' ) { $path = $_SERVER['DOCUMENT_ROOT']; $path .= "/wp-load.php"; require_once($path); global $wpdb; $field_id = 5; $user_query = $wpdb->get_col("SELECT object_id FROM " . $wpdb->prefix . "term_relationships WHERE term_taxonomy_id = " . $field_id); $users_str = ''; foreach ( $user_query as $user ) { $users_str .= $user . ','; } $users_str = rtrim($users_str, ","); $user_ord = $wpdb->get_col("SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE user_id in (" . $users_str . ") AND field_id=18 ORDER BY value ASC"); $user_ordf = ''; foreach ($user_ord as $user_o) { $user_ordf .= $user_o . ','; } $user_ordf = rtrim($user_ordf,","); $ch_querystring['include'] = $user_ordf; $ch_querystring['type'] = 'alphabetical'; $search_terms = $_GET['s']; if(!empty($search_terms)) { $ch_querystring['search_terms'] = $search_terms; } //var_dump($ch_querystring); return $ch_querystring; } else return $querystring; } add_filter( 'bp_ajax_querystring', 'ch_filter_ajax_querystring', 20, 2 );In reply to: Adding new 'Order By' on members loop@shanebp Thank you so much for your help. I got that working. I managed to query based on the new buddypress member types and also an xprofile field which is great.
One weird issue, I can’t see to order them accurately by the xprofile field (which is price). No matter whether I make SQL ORDER BY ASC or DESC the results always appear in the same order in the members loop. The SQL is definitely returning the results in the correct order as I have done a var_dump and checked.
Could it be something to do with
$ch_querystring['type'] = 'price';I have changed this to a different ‘type’ and it does change the results, but not to display them in the order I want.
In reply to: Adding new 'Order By' on members loop@shanebp I thought I shouldn’t be modifying core files, but wasn’t sure how else to do it. I’ll give the above a go and see how it works.
Update 2: I didn’t put the custom profile template in template.php; I put the custom template in profile-loop.php. Wrong?
In reply to: Adding Member Registration Form to New PagesThanks Henry! I’ll get to work on that. I have so many questions about BuddyPress!
Update:
Should I have changed the profile in home.php instead of profile.php? I want to make sure that edit etc. look different. I think when I had edited home.php before I could see all of the changes on edit pages etc.
In reply to: Adding Member Registration Form to New PagesHi @henrywright
I was thinking of just taking the code from register.php. Do you think this would work?
Also, (I commented on this in another thread), where does the data from the form get processed?
I am thinking of adding a hidden field with a member type value in it (Different registration forms for different members) and having a conditional statement when the data gets processed as to whether that member is ‘member type a’, ‘member type b’ etc. and their user roles, but I’m not sure where the data is actually getting processed.
In reply to: Multiple Registration Forms?Hi KSimpson
I’m looking for the same thing. I’m going to be connecting up user roles with ‘member types’ when people register. My issue is having two different member types and allowing them to register separately and the back end automatically assigning the correct roles and types.
What I was thinking was to simply pull out the registration form from register.php and put this into a custom template. I was then thinking to pass a hidden form field with the member type in. Where the form data is processed I will have a conditional statement that says “if $_POST[“expert”] = “expert” then this member type is registered and it’s this role. etc.
My issue is, where is the registration form data processed? It has a blank action attribute so I don’t know where the data goes.
In reply to: Hide Profile Field Group from Specific Member TypeThanks ModemLooper. I’ll give this a shot tonight.
In reply to: How To bp_set_member_type?Would we use something like this to set the user role at registration:
[edited – please use ‘code’ button when sharing code]
function my_set_default_member_type( $user_id, $user_login, $user_password, $user_email, $usermeta ) { // Set the member's type to student bp_set_member_type( $user_id, 'student' ); $user_id->set_role( 'contributor' ); } add_action( 'bp_core_signup_user', 'my_set_default_member_type', 10, 5 );Not sure how we would change the user role when the member type is changed in admin.
In reply to: BuddyPress Users with Tags as TaxonomyThanks Henry! I’ll give that a go and see if it works how I want.
Thanks @djsteveb.
I guess I could do it that way. The layouts are quite different for each of the user types, so I could use an if else on the profile template and just have the code for both in their, not sure if that’s the most efficient way to do it though.