• Resolved Beee

    (@beee)


    I’ve made a listing of all listed users and i’ve excluded the admin user.

    I’ld like to list only authors and contributors and sort it by name, instead of by ID.

    Example is shown on https://www.documentingstyle.com/contributors/?theme=Code

    foreach ( get_users_of_blog() as $author ) :
    			if ($author->ID != '1' ) { ?>
    					<div id="hcard-<?php echo str_replace( ' ', '-', get_the_author_meta( 'user_nicename', $author->ID ) ); ?>" class="author-profile vcard clear">
    
    						<h2 class="author-name fn n">
    							<a href="<?php echo get_author_posts_url( $author->ID ); ?>" title="<?php the_author_meta( 'display_name', $author->ID ); ?>"><?php the_author_meta( 'display_name', $author->ID ); ?></a>
    						</h2>
    						<a href="<?php echo get_author_posts_url( $author->ID ); ?>" title="<?php the_author_meta( 'display_name', $author->ID ); ?>"><?php echo get_avatar($author->ID); ?></a>
    						<p class="author-bio">
    							<?php the_author_meta( 'description', $author->ID ); ?>
    						</p><!-- .author-bio -->
    					</div><!-- .author-profile .vcard -->
    			<?php } endforeach;
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Beee

    (@beee)

    solved it, BUT I can only exclude 1 user… tried several ways to exclude 2 but doesn’t work (yet)

    global $wpdb;
    $query = "SELECT * from $wpdb->users ORDER BY user_nicename";
    $author_ids = $wpdb->get_results($query);
    foreach($author_ids as $author) :
    		if ( $author->ID != '1' ) { ?>
    				<div id="hcard-<?php echo str_replace( ' ', '-', get_the_author_meta( 'user_nicename', $author->ID ) ); ?>" class="author-profile vcard clear">
    
    					<h2 class="author-name fn n">
    						<a href="<?php echo get_author_posts_url( $author->ID ); ?>" title="<?php the_author_meta( 'display_name', $author->ID ); ?>"><?php the_author_meta( 'display_name', $author->ID ); ?></a>
    					</h2>
    					<a href="<?php echo get_author_posts_url( $author->ID ); ?>" title="<?php the_author_meta( 'display_name', $author->ID ); ?>"><?php echo get_avatar($author->ID); ?></a>
    					<p class="author-bio">
    						<?php the_author_meta( 'description', $author->ID ); ?>
    					</p><!-- .author-bio -->
    				</div><!-- .author-profile .vcard -->
    		<?php } endforeach;
    		wp_reset_query();
    		$contributors = new WP_Query('post_type=page&post_parent=106&orderby=title&order=ASC&showposts=-1');
    		while ($contributors->have_posts()) : $contributors->the_post(); ?>
    		<div class="authorbio" id="post-<?php the_ID(); ?>">
    			<p><a href="<?php the_permalink() ?>"><b><?php the_title(); ?></b></a></p>
    			<?php global $more; $more = 0; ?>
    			<p class="foto"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(106,106)); ?></a></p>
    			<?php the_content('read more...'); ?>
    		</div>
    		<div class="clear"></div>
    		<?php endwhile;

    Why not just exclude the users in the main query you have at the top?

    You need not use all the meta function calls either, since you’re already running a query to the users table, the nice name, display name, etc.. is already available in that query..

    I’d use something like this for the first loop ..

    global $wpdb;
    
    $author_ids = $wpdb->get_results( "
    	SELECT ID, user_nicename, display_name
    	FROM $wpdb->users ORDER BY user_nicename
    " );
    
    foreach( $author_ids as $unwanted_key => $author_data ) :
    	if ( 1 == $author_data->ID )
    		// If the ID is 1 skip this result (continue means move onto the next item in the loop - in this case a foreach loop)
    		continue;
    ?>
    	<div id="hcard-<?php echo str_replace( ' ', '-', $author_data->user_nicename ); ?>" class="author-profile vcard clear">
    		<h2 class="author-name fn n">
    			<a href="<?php echo get_author_posts_url( $author_data->ID ); ?>" title="<?php echo $author_data->display_name; ?>">
    				<?php echo $author_data->display_name; ?>
    			</a>
    		</h2>
    		<a href="<?php echo get_author_posts_url( $author_data->ID); ?>" title="<?php echo $author_data->display_name; ?>">
    			<?php echo get_avatar( $author_data->ID ); ?>
    		</a>
    		<p class="author-bio">
    			<?php the_author_meta( 'description', $author_data->ID ); ?>
    		</p>
    		<!-- .author-bio -->
    	</div>
    	<!-- .author-profile .vcard -->
    <?php
    endforeach;

    Excluding is only a case of adding more values to the first condition in the loop..

    if ( 1 == $author_data->ID )

    ..excludes user with the ID of 1

    if ( 1 == $author_data->ID || 2 == $author_data->ID || 3 == $author_data->ID )

    The || is basically an operator for “or” … so in the above example if the user ID is 1,2 or 3 that result is skipped..

    However, forget the conditionals, avoid selecting the unneeded data in the first place by doing the exclude in the actual query, the above was just for illustration..

    eg.

    global $wpdb;
    
    $author_ids = $wpdb->get_results( "
    	SELECT ID, user_nicename, display_name
    	FROM $wpdb->users
    	WHERE ID NOT IN(1,2)
    	ORDER BY user_nicename
    " );
    
    foreach( $author_ids as $unwanted_key => $author_data ) :
    	// No need to have a conditional check here now
    ?>

    1 and 2 are example user IDs.

    Should reduce the total queries.. ??

    Thread Starter Beee

    (@beee)

    I solved it.. and fairly easy…
    I now use the code above with one addition…

    instead of

    if ( $author->ID != '1' ) { ?>

    I use

    if ( $author->ID != '1' && $author->ID != XX ) { ?>

    where XX is ofcourse the user id.
    works like a charm

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to sort a list/query of authors’ is closed to new replies.