Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Top 10 API examples
<?php
/*
* This example fetches the popular posts tracked by Top 10.
*
*/
if ( function_exists( 'get_tptn_pop_posts' ) ) {
$settings = array(
'daily' => TRUE,
'daily_range' => 30,
'limit' => 20,
'strict_limit' => FALSE,
);
$topposts = get_tptn_pop_posts( $settings ); // Array of posts
$topposts = wp_list_pluck( (array) $topposts, 'postnumber' );
$args = array(
'post__in' => $topposts,
'orderby' => 'post__in',
'posts_per_page' => 7,
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query( $args );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
echo '<a href="' . get_permalink( get_the_ID() ) . '">';
the_title();
echo '</a>';
wp_reset_postdata();
}
} else {
}
wp_reset_query();
}
<?php
// Show Top 10 popular posts in Posts Widget.
// Change my_custom_filter with the filter you are using.
add_action( 'elementor_pro/posts/query/my_custom_filter', function( $query ) {
$settings = array(
'daily' => TRUE,
'daily_range' => 30,
'limit' => 20,
'strict_limit' => FALSE,
);
$topposts = get_tptn_pop_posts( $settings );
$posts_in[] = wp_list_pluck( (array) $topposts, 'postnumber' );
// Here we set the query to fetch posts with
// ordered by comments count
$query->set( 'post__in', $posts_in );
$query->set( 'orderby', $posts_in );
} );
<?php
/**
* Add categories to the list items.
*
* @since 2.2.0
*
* @param string $output Formatted list item with link and and thumbnail
* @param object $result Object of the current post result
* @param array $args Array of arguments
*
* @return string Output variable with categories added
*/
function tptn_list_cats( $output, $result, $args ) {
$categories = get_the_category_list( ", ", "", $result->ID );
$output .= " " . $categories;
return $output;
}
add_filter( 'tptn_list', 'tptn_list_cats' , 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.