The function for fetching your most popular posts is called
wp_most_popular_get_popular(). By calling this function, you’ll have your top posts returned to you. It accepts a number of arguments to determine what posts are to be returned.
Here are the arguments that you can pass in to it.
- limit (integer)
- The number of posts you would like to display i.e. 5
- Default: 5
- post_type (string) / (array)
- The post type you would like to display
- Example: post
- Default: All post types
- range (string)
- What date range would you like to display popular posts as
- Accepted: all_time, monthly, weekly, daily
- Default: all_time
Those are the currently supported parameters by the function. Let’s look at an example of how to display the most recent popular posts in a unordered list.
<?php global $post; $posts = wp_most_popular_get_popular( array( 'limit' => 10, 'post_type' => 'post', 'range' => 'all_time', ) ); echo '<ul>'; if ( count( $posts ) > 0 ): foreach ( $posts as $the_post ): setup_postdata( $the_post ); ?> <li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ); ?>"><?php the_title(); ?></a></li> <?php endforeach; endif; echo '</ul>'; wp_reset_postdata();