2

I'm using WP function fetch_feed to retrieve a feed and display its items.

    <?php
    /* include the required file */     include_once(ABSPATH . WPINC . '/feed.php');
    /* specify the source feed   */     $rss = fetch_feed('FEED_URL');
    /* checks obj. is created    */     if (!is_wp_error( $rss ) ) :
    /* specify number of items   */     $maxitems = $rss->get_item_quantity(4);
    /* create an array of items  */     $rss_items = $rss->get_items(0, $maxitems);
endif;
    ?>
    <ul>
        <?php if ($maxitems == 0) echo '<li>Content not available.</li>';
    else
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo $item->get_permalink(); ?>" title="<?php echo esc_html( $item->get_title() ); ?>" rel="external"><?php echo esc_html( $item->get_title() ); ?></a>
            </li>
            <?php endforeach; ?>
    </ul>

If I visit the feed in question with (e.g.) Firefox, I see the entries in the XML appearing order. fetch_feed retrieves the most recent instead (according to their "published" tag). How can I make sure it retrieves items according to their order on the XML feed (not chronologically). Maybe is possible to set the order with this..?

EDIT: I tried adding $feed->enable_order_by_date(false); but it seems to break the function..

6

Ok, found. I spent hours on this but I managed to find the solution. The command I was looking for was $rss->enable_order_by_date(false);.

So you should set (for benefit of the community):

    <?php
    /* include the required file */     include_once(ABSPATH . WPINC . '/feed.php');
    /* specify the source feed   */     $rss = fetch_feed('FEED_URL');
    /* disable order by date     */     $rss->enable_order_by_date(false);
    /* checks obj. is created    */     if (!is_wp_error( $rss ) ) :
    /* specify number of items   */     $maxitems = $rss->get_item_quantity(X);
    /* create an array of items  */     $rss_items = $rss->get_items(0, $maxitems);
endif;
    ?>
    <ul>
        <?php if ($maxitems == 0) echo '<li>Content not available.</li>';
    else
        // Loop through each feed item and display each item as a hyperlink.
        foreach ( $rss_items as $item ) : ?>
            <li>
                <a href="<?php echo $item->get_permalink(); ?>" title="<?php echo esc_html( $item->get_title() ); ?>" rel="external"><?php echo esc_html( $item->get_title() ); ?></a>
            </li>
            <?php endforeach; ?>
    </ul>

Hope this helps! :)

  • Good for you. You can choose your own answer as the correct answer (and maybe explain why when you added it before, it broke the function) – Lea Cohen Feb 3 '13 at 19:35
  • 1
    It broke when I used $feed->enable_order_by_date(false);. It worked with $rss->enable_order_by_date(false); ($rss instead of $feed). :) I can accept my answer in two days. – MultiformeIngegno Feb 3 '13 at 19:45

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.