Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Jason Lawton

Forum Replies Created

Viewing 4 posts - 1 through 4 (of 4 total)
  • Jason Lawton in reply to:
    Sort Author dropdown by Last Name
    #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);
    Jason Lawton in reply to:
    Sort Author dropdown by Last Name
    #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)

    Jason Lawton in reply to:
    Sort Author dropdown by Last Name
    #152221

    Sorry, that first line of code should read:

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

    Jason Lawton in reply to:
    Sort Author dropdown by Last Name
    #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' );
Viewing 4 posts - 1 through 4 (of 4 total)