0

I am trying to get a rss feed that will show the top 10 posts for the week. I am using the top 10 popular posts plugin to show them in a sidebar on the site which works great but need to show them in a rss so that I can have them send in a email that goes out. Below is some code for the top 10 API. Any help would be great.

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

  }
1

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.

| improve this answer | |
  • making a query inside a query is at lieast not efficient. pre_get_post is the right thing to do, as @bitgire had done it in his answer – Mark Kaplun Mar 6 '18 at 4:05
  • sure I just wasn't totally certain without testing, what would happen if you replace the $query value and how that would be done with total confidence, as it is passed by reference to an action in pre_get_posts, whereas in this case it is a filter with a return value. – majick Mar 6 '18 at 7:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.