Forums Forums Search & Filter Pro Sort Author dropdown by Last Name

Tagged: ,

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #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

    Trevor
    #135160

    I am not sure, but it may be possible to use this filter:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    If you search for sf_edit_query_args you will find some recent posts manipulating author in the query, I think.

    Anonymous
    #152205

    Hi, were you able to find a resolution to this? I’m looking at sorting by author last name on my site.

    Anonymous
    #152215

    Yes 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
    #152221

    Sorry, that first line of code should read:

    if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&type=alphabetical-lastname&page=' . $page ) ) :

    Anonymous
    #152256

    Thank you so much. You are really helping me in a pinch. I am new to all of this could you let me know what files I should be modifying to get this to work? Is it a functions.php thing or a specific file in the plugin?

    Anonymous
    #152291

    OMG wait a minute, I’m giving you the wrong code! haha. I think i have it somewhere though. (you can tell sorting by last name is kind of a problem i have repeatedly i guess)

    Anonymous
    #152293

    OK 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);
    Anonymous
    #152306

    Worked perfectly for me after tweaking the user ids and the form ids. Thank you so so so much!! You are a life saver.

Viewing 9 posts - 1 through 9 (of 9 total)