Advertisement
Guest User

users sorted by custom feild value

a guest
Dec 9th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. <?php /*
  2. Template Name: Authors
  3. */ ?>
  4.  
  5. <?php get_header(); ?>
  6.  
  7.  
  8. <div id="container">
  9.  
  10.  
  11. <div id="content">
  12.  
  13.  
  14. <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
  15.  
  16. <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
  17.  
  18. <h2 class="entry-title"><?php the_title(); ?></h2>
  19.  
  20.  
  21.  
  22. <div class="entry-content">
  23. <?php the_content(); ?>
  24.  
  25.  
  26.  
  27.  
  28.  
  29. <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
  30. <?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
  31. </div><!-- .entry-content -->
  32. </div><!-- #post-## -->
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40. <?php endwhile; // end of the loop. ?>
  41.  
  42.  
  43.  
  44.  
  45. <?php
  46.  
  47.  
  48. // Show a selected number of posts per row
  49. $posts_per_row = 5;
  50.  
  51.  
  52.  
  53. if (have_posts()) {
  54.  
  55. global $wpdb;
  56.  
  57. $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users WHERE display_name <> 'admin' ORDER BY display_name");
  58.  
  59.  
  60.  
  61.  
  62. //these are the arguments for the get_users function below
  63. $args = array(
  64. 'fields' => 'all_with_meta',
  65. 'meta_query' => array(
  66. array(
  67. 'key' => 'twitter', // the meta field (or key) we want to target
  68. 'value' => 'Ihavenotweets', // the value we want to target (optional)
  69. 'compare' => '>=' // comparison method (optional: =, >, <, etc)
  70. )
  71. ));
  72.  
  73. //get_users calls WP_User_Query and returns an array of matching users
  74. $users = get_users($args);
  75.  
  76. //custom function for comparing the data we want to sort by
  77. function cmp($a, $b){
  78. if ($a->points == $b->points) {
  79. return 0;
  80. }
  81. return ($a->points > $b->points) ? -1 : 1;
  82. }
  83.  
  84. //usort sorts our $users array with our function cmp()
  85. usort($users, 'cmp');
  86.  
  87. //leaving an array of $users sorted by the value of meta 'points'
  88.  
  89.  
  90.  
  91.  
  92. foreach ($users as $author ) :
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. // Get user data
  100. $curauth = get_userdata($author->ID);
  101. $curauth->user_email;
  102.  
  103.  
  104. // Get link to author page
  105. $user_link = get_author_posts_url($curauth->ID);
  106.  
  107. //show authors with posts
  108. $post_count = get_usernumposts($curauth->ID);
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121. if (!$post_count) {
  122.  
  123. } else
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. while (have_posts()) {
  138. the_post();
  139. if ((++$post_counter % $posts_per_row) == 1 || $posts_per_row == 1) {
  140. if ($post_counter > 1) {
  141. echo
  142.  
  143. "</div><!-- End of post_row -->\n"; // End previous row
  144. }
  145. echo "<div class='post_row3'>\n"; // Start a new row
  146. }
  147. echo "<div
  148.  
  149. class='post_class3'>\n"; // Start one post
  150.  
  151.  
  152.  
  153. // Output post data here
  154. ?>
  155.  
  156.  
  157. <div class="entry-meta">
  158. <a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>"><?php echo $curauth->display_name; ?></a>
  159. </div>
  160. <a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>">
  161. <?php echo get_avatar($curauth->user_email, '96', $avatar); ?></a>
  162.  
  163.  
  164.  
  165.  
  166. <?php //endwhile
  167. echo "</div><!-- End of post_class -->\n"; // End of post
  168. } ?>
  169.  
  170.  
  171.  
  172. <?php endforeach; ?>
  173.  
  174.  
  175.  
  176.  
  177.  
  178. <?php //endif
  179. } ?>
  180.  
  181.  
  182.  
  183. </div><!-- End of post_row -->
  184.  
  185.  
  186.  
  187. </div>
  188.  
  189.  
  190. </div><!-- #container -->
  191.  
  192. <?php get_sidebar();?>
  193. <?php get_footer(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement