You could do it by overriding the main query results if there is a querystring detected in the feed URL eg. ?top_ten
Add a query variable for the top ten:
function my_query_vars( $query_vars ) {
$query_vars[] = 'top_ten';
return $query_vars;
}
add_filter( 'query_vars', 'my_query_vars' );
Create a function for your code to retrieve the top ten as a query
function my_top_ten_query($limit, $query) {
if ( !function_exists( 'get_tptn_pop_posts' ) ) {return $query;}
$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' => $limit,
'ignore_sticky_posts' => 1
);
$my_query = new WP_Query( $args );
return $my_query;
}
Then filter the main query to check for the querystring and return your top ten query instead of the standard feed query:
function my_feed_posts_filter( $query ) {
// only for feeds
if ( $query->is_feed && $query->is_main_query() ) {
// check if the top_ten variable is set
if ( isset( $query->query_vars['top_ten'] )
&& ! empty( $query->query_vars['top_ten'] ) ) {
// return your top ten query object instead
return my_top_ten_query($query->query_vars['top_ten'], $query);
}
}
return $query;
}
add_filter( 'the_posts', 'my_feed_posts_filter' )
Solution adapted from this answer on custom feed queries.