A code that I could not fully grasp related to foreach & count_user_posts()


Working on my first WordPress theme, I created a page template for authors based on this tutorial which is itself based on the twenty-fourteen theme.

What I could not understand is this :

$post_count = count_user_posts( $contributor_id );

From this code example (check especially the first 10 lines for my first question which will very possibly automatically answer my final questions):

// Output the authors list.
$contributor_ids = get_users( array(
  'fields'  => 'ID',
  'orderby' => 'post_count',
  'order'   => 'DESC',
  'who'     => 'authors',

foreach ( $contributor_ids as $contributor_id ) :
$post_count = count_user_posts( $contributor_id );
  // Move on if user has not published a post (yet).
  if ( ! $post_count ) {

<div class="contributor">
  <div class="contributor-info">
    <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
    <div class="contributor-summary">
      <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
      <p class="contributor-bio">
        <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
      <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
        <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
    </div><!-- .contributor-summary -->
  </div><!-- .contributor-info -->
</div><!-- .contributor -->


I have checked the Codex for count_user_posts( ) but I could not understand how the code is aligned with the documentation as the output of the foreach loop $contributor_id is not only the ID but also post_count,desc and authors.

These are supposed to be the second parameters which are optional and are

Single post type or array of post types

as the Codex specifies below:

$post_type (array|string) (Optional) Single post type or array of
post type
s to count the number of posts for. Default value: ‘post’

So how the values of “post_count” ,”desc” and “authors”. fit with the Codex documentation?

Same question for:

get_avatar( $contributor_id, 132 )


get_the_author_meta( 'display_name', $contributor_id )
FakhriAz 4 years 2020-02-18T08:38:25-05:00 0 Answers 75 views 0

Leave an answer