Top 10 v3.0.0 introduced Top_Ten_Query, which works as a wrapper for WP_QueryOpens in a new window. This brings all the power and flexibility of WP_Query to Top 10. If you’re not familiar with WP_Query, I recommend reading the documentationOpens in a new window.

Standard Loop

<?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();

get_tptn_posts()

get_tptn_posts() is a wrapper to Top_Ten_Query. You can use it to retrieve an array of the popular posts. It also accepts the same $args as Top_Ten_Query.

Parameters

In addition to the WP_Query parametersOpens in a new window, Top_Ten_Query also takes these additional parameters.

ParameterTypeDescription
blog_idarray or stringArray or comma-separated string of blog IDs.
dailyboolSet to true to fetch daily or custom period posts. Set to false for the overall popular posts.
daily_rangenumberEnter the number of days, e.g. set it to 7 to fetch the popular posts in the past week.
include_cat_idsarray or stringArray or comma-separated string of categories or term_taxonomy_ids.
include_post_idsarray or stringArray or comma-separated string of post IDs to include.
offsetintOffset the related posts returned by this number.
strict_limitboolIf false, fetches up to 3× the limit to allow filtering.

Was this article helpful?

Tagged: