Top 10 v3.0.0

I’m pleased to say that I have now pushed Top 10 v3.0.0 into the WordPress repository. This is a major release of the plugin and brings a series of massive features that I hope you like. While I have tested this significantly, including running it live on a few of my sites, I’m conscious that there might be some bugs. Please let me know about any bugs by posting in the support forum.

After upgrading, I strongly suggest visiting the settings page, rechecking them and then resaving the settings. If you’re using custom code that filters the SQL query that is created by older versions of the plugin, then you will need to migrate your code to get this to continue working with v3.0.0.

Introducing Top_Ten_Query

Prior to v3.0.0, Top 10 used get_tptn_pop_posts() to fetch popular posts. This was a custom function that worked well but, in my view, limited the flexibility of the plugin particularly for power users and devs. Given that most devs are familiar with WP_Query, it made sense to create a new lookup function that wrapped around this.

This version introduces Top_Ten_Query, a new class that brings the power of WP_Query to Top 10. In addition to this function, you can also use get_tptn_posts() to only fetch the popular posts. All the posts are returned as an array of WP_Post objects with an additional field called visits which holds the overall count of the post. This is a breaking change from earlier where the counts were fetched from the database as sum_count.

For more advanced users, you can now use Top_Ten_Query just as you would WP_Query. e.g. this gist below shows what a standard loop would look like.

<?php
 
// The Query.
$the_query = new Top_Ten_Query( $args );
 
// The Loop.
if ( $the_query->have_posts() ) {
	echo '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<li>' . get_the_title() . '</li>';
	}
	echo '</ul>';
} else {
	// no posts found.
}
/* Restore original Post Data */
wp_reset_postdata();

This change also means that most of the filters within get_tptn_pop_posts() have now been deprecated as they are no longer used by the core plugin. I will eventually deprecate this function in a future version and it will throw up a notice.

You can now use the WP_Query filters like posts_where, posts_join, etc. to use customise the SQL query. I recommend checking for the custom parameter top_ten_query to makes sure that you are filtering a Top_Ten_Query clause.

// Return if it is not a Top_Ten_Query.
if ( true !== $query->get( 'top_ten_query' ) ) {
    return $where;
}

Gutenberg Block

This was a long time coming. Top 10 v3.0.0 brings a Gutenberg block that allows you to display the popular posts within the post content or in your page builder if it allows for Gutenberg blocks. You can find it by searching for Popular Posts or Top 10. You have several settings for the block that allows you to choose how many posts to display, show headings, authors, etc. You also have the Other attributes field that allows you to pass query strings of options for more advanced users.

Top 10 Gutenberg block

Giving it a REST

Top 10 now supports WordPress REST API. The plugin adds two new routes. The main one is wp-json/top-10/v1/popular-posts/ which allows your custom app to pull the popular posts by making a GET request to this route similar to how you would use the posts endpoint.

Additionally, you can pass multiple parameters similar to the Top 10 shortcode that allows you to customise which posts to pick.

Additionally, you can also retrieve a specific post by querying wp-json/top-10/v1/popular-posts/<id> which includes a standard WP_Post object but also includes a field called visits.

Additionally, you’ll find a new tracker type called REST API based under the Counter/Tracker tab on the settings page. As a reminder, if you have a busy site, you can use the Fast Tracker which should be compatible with most standard installs.

New Dashboard

Navigating to Top 10 in the Admin page will now display the new Dashboard. It includes a nice graph using ChartJS to display the number of visits to your site on a daily basis. You can also see the top posts for today, yesterday, last 7 days, last 30 days and overall.

Detailed changes

  • Features:
    • New Top_Ten_Query class for fetching popular posts. Adds the function get_tptn_posts() which replaces get_tptn_pop_posts() which will be deprecated in a future version
    • New option to exclude the Front page and Posts page if these are set in Settings > Reading or via Customizer
    • New Dashboard which displays the total hits daily in a chart as well as the most popular posts today, yesterday, last 7 days and last 30 days
    • New option in the Widget to include specific post IDs in the top lists. You can also use them in the shortcode using include_post_ids
    • New block for Gutenberg aka the block editor. The block is called Popular Posts [Top 10] and you can find it under the widgets category
    • Top 10 now supports the WP REST API. The plugin adds a new tracker type called REST API based which you can find under Counter/Tracker settings. Additionally, you can now receive the popular posts via a REST Request to top-10/v1/popular-posts
  • Enhancements/Modifications:
    • No popular posts feed will be added if the corresponding slug is set to blank
    • Changed sum_count to visits
  • Bug fixes:
    • PHP notices when displaying Network Wide Popular Posts in WordPress Multisite
    • Query based tracker gave an ajax error

Download Top 10

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *