• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Display a list of the latest posts in a WordPress installation using the REST API

Discover in this tutorial how to get and display the latest posts from a remote WordPress installation using the REST API.

In this tutorial you are going to learn how you can use the REST API to fetch and display a list of the latest posts from an external WordPress installation. This would be the result:

list-last-entries-installation-WordPress-external-API-REST
Latest posts from OsomPress

Let’s see it:

Instructions for displaying the latest posts from a WordPress installation using the REST API

To get the information about the latest posts you are going to use the REST API endpoint that gives you access to that information.

In addition, you are going to encapsulate this data inside a shortcode so you can display the listing wherever you want.

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 847 code snippets of this website, login or subscribe here.

With this code you create a shortcode that allows you to display a list with all the posts of a remote WordPress installation using the [post_list] shortcode.

In the first part of the snippet you define the variables:

  • $website_url = The URL of the website
  • $request_url= The URL where the request is made. In this case it is the concatenation: $website_url . '/wp-json/wp/v2/posts/' which accesses the endpoint for the REST API entries.

Remember to change the value of the following variables to those of your case.

Then you use the wp_remote_request function to get the endpoint for posts (with the GET method).

Once you get the information sent by the API you use the functions wp_remote_retrieve_body to extract the body data and json_decode to decode it.

And when you have the array with the data, you use a loop (foreach) to extract the information you are interested in.

In this case, the title [title->rendered] > with link to it [link].

But you can add more to customize it to your liking. These are all the details you can display:

  • [date]
  • [date_gmt]
  • [guid]
  • [id]
  • [link]
  • [modified]
  • [modified_gmt]
  • [slug]
  • [status]
  • [type]
  • [password]
  • [permalink_template]
  • [generated_slug]
  • [title]
  • [content]
  • [author]
  • [excerpt]
  • [featured_media]
  • [comment_status]
  • [ping_status]
  • [formart]
  • [meta]
  • [sticky]
  • [template]
  • [categories]
  • [tags] [tags]

You have more information about it on official WordPress developer page.

So you can customize the details you prefer to be displayed.

If you want to see this data you can use the print_r function, in this case it would be return print_r($remote_post);

If you have any questions or suggestions you can leave them in the comments.

To be able to see the list you only have one step left.

2. Insert the shortcode where you want

The advantage of shortcodes is that you can insert them anywhere on the web.

You only have to type [post_list] where you want it to be displayed.

Important:

  • If you are using the classic editor, use the HTML tab to insert the shortcode.
  • In case you are already using the new block editor (Gutenberg) you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.

This would be an example of the listing that the shortcode would display:

list-last-entries-installation-WordPress-external-API-REST
Latest posts from OsomPress

Conclusions

Now you know how to use the REST API to fetch and display the latest posts from a remote WordPress installation.

If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.

Benefits of being a subscriber. 🙂

PHP REST API Shortcode WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?