3

I am trying to get the 2 latest posts from my personal website, using the code below from http://codex.wordpress.org/Function_Reference/fetch_feed#Usage

<h2><?php _e( 'Recent news from Some-Other Blog:', 'my-text-domain' ); ?></h2>

<?php // Get RSS Feed(s)
include_once( ABSPATH . WPINC . '/feed.php' );

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed( 'THISISWHEREMYURLGOES/' );

$maxitems = 0;

if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly

    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity( 2 ); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items( 0, $maxitems );

endif;
?>

<ul>
<?php if ( $maxitems == 0 ) : ?>
    <li><?php _e( 'No items', 'my-text-domain' ); ?></li>
<?php else : ?>
    <?php // Loop through each feed item and display each item as a hyperlink. ?>
    <?php foreach ( $rss_items as $item ) : ?>
        <?php echo esc_html( $item->get_title() );  ?>
        <li>
            <a href="<?php echo esc_url( $item->get_permalink() ); ?>"
                title="<?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>">
                <?php echo esc_html( $item->get_title() ); ?>                    
                <?php printf( __( 'Posted %s', 'my-text-domain' ), $item->get_date('j F Y | g:i a') ); ?>

            </a>
        </li>
    <?php endforeach; ?>
<?php endif; ?>

With this code, I can get the Posts URL, the title and the date posted, which is great!

Now, trying to get the image is another issue. I am trying to use :

<?php echo esc_html( $item->the_post_thumbnail() ); ?> 

But I get the error : Fatal error: Call to undefined method SimplePie_Item::the_post_thumbnail()

So, using SimplePie, is there a way to get the posts image?


MAJOR EDIT:

This way of getting the RSS feed isn't great, it is causing alot of issues throughout the site, so if anyone could show me/direct me to something where I can get the 4 latest posts from another WordPress site, that'd be awesome!

2 Answers2

6

As you've found, WordPress feeds have some limitations. Since you've asked for an alternative solution, I'd definitely recommend using WP REST API.

Since WP API isn't yet part of the WP Core, you'll want to do the following:

  1. Head to your Plugins panel (on the site you're trying to pull posts from...your personal website) and install WP REST API (WP API).
  2. Activate the plugin
  3. Getting your posts is as easy as going to: http://yoursite.com/wp-json/posts

Since you only want four posts, you can use filters:

http://yoursite.com/wp-json/posts?filter[posts_per_page]=4

To get this JSON into a usable state in PHP:

// Get the JSON
$json = file_get_contents('http://yoursite.com/wp-json/posts?filter[posts_per_page]=4');
// Convert the JSON to an array of posts
$posts = json_decode($json);

You can now digest this $posts array however you want (by looping through it). For example:

foreach ($posts as $p) {
    echo '<p>Title: ' . $p->title . '</p>';
    echo '<p>Date:  ' . date('F jS', strtotime($p->date)) . '</p>';
    // Output the featured image (if there is one)
    echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
}

More info in the WP API docs.

rnevius
  • 26,578
  • 10
  • 58
  • 86
  • 1
    It will be great to have this supported by the core - PS: we should also notice that the plugin is still evolving and we got for example this notice from the team on version 2 beta: ["Development Only: Due to the lack of forwards and backwards compatibility, we strongly recommend you do not run the beta in production environments."](https://make.wordpress.org/core/2015/04/29/wp-rest-api-version-2-0-beta-1/). The latest version of the plugin in the repository on wordpress.org is 1.2.x. – birgire Jun 25 '15 at 12:57
  • @birgire, yes, I've been waiting patiently for the merge. Until then, the official plugin has been great! Thanks for adding that important note. – rnevius Jun 25 '15 at 12:59
  • I followed these steps, activated the plugin on the site I want the posts FROM, and then on the site I want to pull these posts through, used this code: But nothing displays at all? –  Jun 25 '15 at 13:27
  • Nothing should display. As I mentioned, you're going to need to loop through the array. You've stored the posts as an array in the `$posts` variable. If you want to see all of the data that is contained in that array, you can add a `var_dump($posts)`. – rnevius Jun 25 '15 at 13:32
  • I just get 'NULL' when I try var_dump($posts). Guessing this means It isnt actually getting any information? –  Jun 25 '15 at 13:36
  • Yes, I see what the issue is now...Your URL is wrong. You need to have it in the form of `/wp-json/posts/?filter[posts_per_page]=4`. – rnevius Jun 25 '15 at 13:37
  • Feel free to [join me in chat](http://chat.stackoverflow.com/rooms/81536/wp-json) if you'd like to try to figure out this URL issue. – rnevius Jun 25 '15 at 13:39
  • Great, that shows all the information, is there some documentation which will clearly explain as to how to get 1) The title of the post 2) The image 3) The post date and time –  Jun 25 '15 at 13:39
  • @rnevius can we get post from specific post type too?? for example i have post type "event" so i can get posts of this only. – Xabby Dec 02 '16 at 16:13
  • I second that question from @Xabby is there a way to filter with the custom post type, categories or tags? – Michael G Feb 27 '18 at 17:19
-1

If you dont want to use WP REST API, you can give a try to Wordpress Developers API.

You will have to authorize Wordpress Jetpack plugin for this. And then enable the REST API.

<?php
$posts = json_decode(file_get_contents("https://public-api.wordpress.com/rest/v1.1/sites/{yoursite.com}/posts"));
//You can use the $posts variable afterwards
?>
Manan
  • 929
  • 9
  • 11
  • @rnevius You can use the API with self hosted blogs also. You have to authorize the Jetpack plugin for this. – Manan Jun 28 '15 at 02:39