How to Display Most Popular Posts using Views Post Meta in WordPress

Home » Snippets » How to Display Most Popular Posts using Views Post Meta in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Want to display your most popular posts based on views? This can be a powerful tool to engage your audience by showing them what’s already creating a buzz on your website. By using post views post meta, you can easily sort and display your posts based on popularity. However, achieving this isn’t always clear-cut. That’s why in this article, we’ve made it easy and will show you exactly how to use views post meta to display your most popular posts on WordPress.

					function wpturbo_most_popular_posts( $number = 5 ) {
    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => $number,
        'meta_key'       => 'post_views',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
    );
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            the_title();
            echo '<br>';
        }
        wp_reset_postdata();
    }
}
add_action( 'wp_footer', 'wpturbo_most_popular_posts' );
```
This snippet will display the titles of your most popular posts (based on the `post_views` meta key) in the footer of your site. You can change 'wp_footer' to any other hook based on where you want the popular posts to be displayed.
				

The first line of the code snippet generates a function named wpturbo_most_popular_posts(), with an optional parameter $number which defaults to 5. This function will be responsible for fetching and displaying the most popular posts on your WordPress site.

Firstly, an array of arguments ($args) is defined which is later used in a new instance of the WP_Query class.

    $args = array(
        'post_type'      => 'post',
        'posts_per_page' => $number,
        'meta_key'       => 'post_views',
        'orderby'        => 'meta_value_num',
        'order'          => 'DESC',
    );

Here, post_type is set to ‘post’, which tells WordPress we’re interested in posts only. Then, the number of posts per page (posts_per_page) is set to $number, which we initially set to 5.

The meta_key is set to ‘post_views’, which is the custom field that stores the view count for each post. The orderby key is set to ‘meta_value_num’, which means the posts will be ordered by numerical value of the ‘post_views’ custom field. Lastly, the order key is set to ‘DESC’, indicating we want the posts with the highest view count to appear first.

The next step involves running a new instance of WP_Query with the $args array as an argument:

$query = new WP_Query( $args );

Now we move on to checking if the query has posts and then looping through the posts. For each post, we display the title of the post followed by a line break <br>.

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        the_title();
        echo '<br>';
    }

After displaying the posts, it’s important to reset the post data. This is to ensure that our custom query does not interfere with other loops or post-related functions on the page.

wp_reset_postdata();

Lastly, we’ll hook our wpturbo_most_popular_posts() function into the wp_footer action, which simply means that the function executes right before closing the </body> tag.

add_action( 'wp_footer', 'wpturbo_most_popular_posts' );

This will cause our most popular posts to be displayed in the footer of the site. However, you can choose a different hook, depending on where you want the list to appear.

Register an account to save your snippets or go Pro to get more features.