WordPress Popular Posts Tutorial
Anytime we have a client or if us ourselves are looking for a custom feature we first look on google for any and all sites that may a have a quick tutorial. But after search for some-while we didn’t find a quick a easy solution for for this feature.
We searched for: WordPress Popular Posts Sidebar Widget, WordPress Popular Posts and WordPress Popular Posts Plugin.
We found many solutions, plugins, widgets, snippets and none were quick down and dirty examples.
So here is our quick, down and dirty WordPress Popular Posts in Sidebar Tutorial (long enough title for you?).
Before we start
You need to know the following before preceding. Make sure you have experience in the following before trying this tutorial:
- PHP 5 or newer
- WordPress Database structure (just an understanding of how tables are laid out is necessary)
- mySql (basic understanding a plus)
In a nutshell:
What we are going to do is anytime someone views a post (article) we will add to a custom meta called views. Then in our sidebar, custom template or wherever you want to add this query; we will query only posts with the custom meta views and order them based on the most popular. Simple!!
Step One:
In your current them open your single template (single.php) in your favorite IDE (text editor, dreamweaver, etc), this is the template that displays the actual post content, same as what you are reading now. Add the following code above the get_header() function at the top of your document.
global $post; if ( !is_user_logged_in() ){ $currentViews = ( (int)get_post_meta( $post->ID, 'views', true ) + 1 ); update_post_meta( $post->ID, 'views', $currentViews ); }
Let me explain:
All this does is make sure the $post object is available for us so we can use if necessary.
global $post;
We want to make sure that we don’t add a view if the user is logged in so we add this if statement.
if ( !is_user_logged_in() ){ }
Last for step one we need to get the current views of this post if any and update that number.
// get current views and update $currentViews = ( (int)get_post_meta( $post->ID, 'views', true ) + 1 ); // update the views custom meta update_post_meta( $post->ID, 'views', $currentViews ); }
Step Two:
Look back in your current theme folder and open the template file that you want to add the popular posts query to. We want it in our siderbar.php template so that it shows up in our sidebar. Add the following code to your template.
$args = 'post_type=post&posts_per_page=3&orderby=meta_value&meta_key=views&order=DESC'; query_posts( $args ); if(have_posts()): while(have_posts()): the_post(); ?> <? the_title(); ?> <? the_excerpt(); ?> <a href="<? the_permalink();">Read More</a> <? endwhile; endif; wp_reset_query();
Thats it!! I don’t think there is a need to explain the above code, its a normal query all you need to do is change this value posts_per_page=3 in the above query to reflect how many posts you want to show up.
For use with custom post types instead of posts
All you have to do is;
- For step one add the code that updates the views to the single-post-type.php template of your choice
- change the following query args ‘post_type=post&posts_per_page=3&orderby=meta_value&meta_key=views&order=DESC’. Change post_type=post to post_type=your post type name.
That is it, if you have questions of comments please post them below. If you need immediate support click here for paid support.