Like the blog? Get the book »

Popular Posts Shortcode

Popular Posts Shortcode

In my recent post, DIY WordPress Popular Posts, I share a simple, two-step technique for tracking and displaying popular posts on your WordPress-powered site. That post describes everything needed to fully implement DIY popular posts, but some folks wanted an easier (more convenient) way to display the list of popular posts on the front-end (instead of using template code).

And as a bonus, make it possible to specify the category and number of posts. So in this quick follow-up tutorial, I share a sweet little shortcode that does exactly that: displays a customizable list of popular posts.

Hello, Shortcode

The Popular Posts Shortcode is entirely plug-&-play with no configuration or editing required. Simply add the following code to your theme’s functions.php file and you’re ready to go. Note that this shortcode requires that DIY Popular Posts is implemented on your site. Here’s the secret sauce:

// shortcode: display diy popular posts: [diy_pop_posts num="10" cat="1,2,3"]
function shapeSpace_display_popular_posts($atts) {
	
	extract(shortcode_atts(array(
		'num' => 10,
		'cat' => '',
	), $atts)); 
	
	$temps = explode(',', $cat);
	$array = array();
	foreach ($temps as $temp) $array[] = trim($temp);
	
	$cats = !empty($cat) ? $array : '';
	
	?>
	
	<h3>Popular Posts</h3>
	<ul>
		<?php $popular = new WP_Query(array('posts_per_page' => $num, 'meta_key' => 'popular_posts', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'category__in' => $cats));
		while ($popular->have_posts()) : $popular->the_post(); ?>
		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php endwhile; wp_reset_postdata(); ?>
	</ul>
	
<?php }
add_shortcode('diy_pop_posts', 'shapeSpace_display_popular_posts');

This code snippet uses the WP API to create a shortcode called [diy_pop_posts] that can be used on any WordPress Post or Page (or CPT). The function uses WP_Query to grab and display a simple unordered list <ul> of all matching popular posts. Bada bing, bada boom.

Shortcode Usage

Once the DIY technique and shortcode are in place, you can display a list of the most popular posts on your site. Here are some examples of shortcode usage:

[diy_pop_posts]                      // displays top 10 popular posts from all categories
[diy_pop_posts num="100"]            // displays top 100 popular posts from all categories
[diy_pop_posts num="5" cat="1,2,3"]  // displays top 5 from categories 1, 2, and 3
[diy_pop_posts cat="1,5"]            // displays top 10 from categories 1 and 5

So it’s all pretty straightforward. If you have any questions or suggestions feel free to share in the comments section below, or send via email.

6 responses

  1. Is it possible with this (and your previous template version) to us this with specific custom post types rather than just normal posts?

  2. Nice tutorial, thanks for sharing :)
    Can we go further than that and set a transient to store the popular posts for a while? Let`s say for 24 hours.
    I guess it`s a convinient amount of time to update the transient.
    It would be a nice to have feature :D

    Thank you again!

  3. When using AdBlock Plus the PP widgets keep loading.

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace