Forums › Forums › Search & Filter Pro › Sort Author dropdown by Last Name
- This topic has 8 replies, 3 voices, and was last updated 6 years, 10 months ago by Anonymous.
-
Anonymous(Private) October 6, 2017 at 5:10 pm #135157
Is it possible to sort the author dropdown by last name? There are obviously a bunch of options, but none of them are *exactly* what I need.
Would it be possible to do with a hook? I’m not sure what hook name it would be, so if you can point me in the right direction with that I could probably figure it out 🙂
Thanks
Anonymous(Private) January 12, 2018 at 4:05 pm #152215Yes I was! I created a ‘alphabetical-last-name’ and pass it as the ‘type’ in the query string:
if ( bp_has_members( bp_ajax_querystring( 'members' ) . $str . $qs ) ) :
And then I have a hook on
bp_pre_user_query
like so:function alphabetize_by_last_name( $bp_user_query ) { if ( 'alphabetical-lastname' == $bp_user_query->query_vars['type'] ) { // SELECT u.ID as id FROM wp_users u $bp_user_query->uid_clauses['select'] = "SELECT u.ID as id FROM wp_users u LEFT JOIN wp_bp_xprofile_data xpd ON u.id = xpd.user_id"; // xpd.field_id 3 is my "last name" field in buddypress extended fields $bp_user_query->uid_clauses['where'] = "WHERE xpd.field_id = 3"; $bp_user_query->uid_clauses['orderby'] = "ORDER by xpd.value"; $page = $bp_user_query->query_vars['page']; if ($page) { $offset = 20; $limit = ( $page - 1 ) * $offset; $bp_user_query->uid_clauses['limit'] = "LIMIT $limit, $offset"; } } } add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' );
Anonymous(Private) January 12, 2018 at 7:18 pm #152293OK Here it is. I needed to show the author name by last, first, and sometimes the username by last,first. So I have a hook (which yes, you put in functions.php) and I check the form id and the field we’re outputting, and process it based on what the values are.
If we’re looking at the author field, i basically requery the entire author list, and regenerate the list completely. You can see I exclude some authors (like admin, and some other random people i didn’t want in the list)
If it’s the user field, i do a loop and requery the user and then adjust the info.
I think i wrote the two if statements at different times, because the second way could probably be applied to the first. Whatever, it works.
function sf_edit_author_field_order($input_object, $sfid) { if( in_array( $sfid, array( 3390, 3444, 3562 ) ) && $input_object['name'] == '_sf_author' ) { // requery the author list $author_args = array( 'exclude_admin' => true, 'hide_empty' => true, 'meta_key' => 'last_name', 'orderby' => 'meta_value', 'order' => 'ASC', 'exclude' => array( 429, 233, 558, 7 ) ); $user_query = new WP_User_Query( $author_args ); $users = $user_query->get_results(); $new_users = array(); $new_users[] = (object) array( 'label' => 'All Authors', 'attributes' => array ( 'class' => 'sf-level-0 sf-item-0' ), 'value' => '', 'count' => 0 ); // go through users and get their user_nicename and display_name foreach( $users as $user ) { $new_users[] = (object) array( 'attributes' => array( 'class' => 'sf-level-0' ), 'value' => $user->user_nicename, 'label' => $user->last_name . ', ' . $user->first_name, 'count' => 1 ); } $input_object['options'] = $new_users; } else if ( $sfid == 3444 && $input_object['name'] == '_sft_userlist' ) { // requery the tagged user list to get their names - last, first // go through the options part of the object and get the user id, and then the meta info for that foreach ($input_object['options'] as $option) { // get the user id from the term meta $temp_term = get_term_by( 'slug', $option->value, 'userlist'); $temp_term->meta_user_id = get_term_meta( $temp_term->term_id, 'user_id', true ); $last_name = get_user_meta( $temp_term->meta_user_id, 'last_name', true ); $first_name = get_user_meta( $temp_term->meta_user_id, 'first_name', true ); // overwrite the original label if ( $last_name && $first_name ) { $option->label = $last_name . ', ' . $first_name; } } } return $input_object; } add_filter('sf_input_object_pre', 'sf_edit_author_field_order', 10, 2);
-
AuthorPosts