A quick guide to fetching WordPress posts with REST API
- Using the WordPress documentation, create an endpoint to retrieve the list of posts to display on your website within Divhunt.
- Connect this endpoint to Divhunt via the REST API.
- Link the data from the API to the elements inside the builder.
How to retrieve WordPress posts (Video tutorial)
In the video tutorial below, you will learn how to retrieve all posts from WordPress and display them on your Divhunt website. The video demonstrates how to connect all blog posts and a single post page. For this specific post, watch the first 10 minutes of the tutorial.
Customize your WordPress endpoint for precise data retrieval
This post covers the essential endpoints and arguments you'll need. For more information, consult the extensive WordPress documentation.
Default endpoint
https://yourwebsite.com/wp-json/wp/v2/postsThe default endpoint shown above retrieves the 10 most recent posts from WordPress, including all standard data provided by WordPress. Additionally, some plugins may add extra information to each post. For example, the Yoast SEO plugin adds a lot of data to each post item in this API.
Additonal queries
With WordPress, you can manipulate the data you receive from the API using arguments. For example, you can filter posts by specific category, limit the number of posts, set a date range, sort the results, and much more.
Posts amount (per_page)
https://yourwebsite.com/wp-json/wp/v2/posts?per_page=50Retrieves custom amount of posts. By default, this is set to 10.
Get only specified fields (_fields)
https://yourwebsite.com/wp-json/wp/v2/posts?_fields=id,slug,title,excerpt,contentRetrieve only the specified fields from each post. This makes your API calls faster and helps you stay organized when working with data inside the builder.
Order
https://yourwebsite.com/wp-json/wp/v2/posts?orderby=title&order=ascWhen working with ordering, there are two arguments: orderby and order. The order argument lets you specify whether you want the results in ascending (A-Z, oldest to newest) or descending (Z-A, newest to oldest) order.
The orderby argument allows you to decide the criteria for sorting:
- date: This is default value. Orders posts by their creation date.
- title: Orders posts alphabetically by title.
- id: Orders posts by their ID.
- slug: Orders posts alphabetically by their slug.
- author: Orders posts alphabetically by their author.
- modified: Orders posts by the last modified date.
- comment_count: Orders posts by the number of comments.
Using Multiple Arguments in WordPress API Queries
To use more than one argument in your WordPress API queries, you need to combine them using the & symbol.
- Start with a ?: Begin your query with a question mark ? to start adding arguments.
- Add the first argument: For example, per_page=5 to get 5 posts.
- Combine with &: Use the & symbol to add more arguments.
- Add additional arguments: For example, order=asc and orderby=title to sort the posts.
https://yourwebsite.com/wp-json/wp/v2/posts?argument1=value1&argument2=value2
https://yourwebsite.com/wp-json/wp/v2/posts?per_page=5&order=asc&orderby=titleAdd category details and featured image to endpoint
By default, WordPress provides only category IDs and the ID of the featured image. However, in most use cases like ours, we need more details for each category, such as the name, slug, and ID. For the featured image, we need the URL to display it within the builder. Therefore, we need to manually push this information from WordPress.
To do this, follow these steps:
- Open your wp-admin, locate the tools > theme file editor, and then open functions.php file of your template. Here paste two functions from below.
- Paste the following two functions into the functions.php file:
- Save the changes.
Note: Depending on your website setup, you might not have access to theme code files from the WordPress admin panel. In this case, you would need to use FTP to access and modify the functions.php file.
function add_cover_image_to_posts( $data, $post, $context ) {
$featured_image_id = get_post_thumbnail_id( $post->ID );
if ( $featured_image_id ) {
$image_url = wp_get_attachment_image_url( $featured_image_id, 'full' );
$data->data['cover_image_url'] = $image_url;
} else {
$data->data['cover_image_url'] = '';
}
return $data;
}
add_filter( 'rest_prepare_post', 'add_cover_image_to_posts', 10, 3 );
function add_category_details_to_posts( $data, $post, $context ) {
$categories = get_the_category( $post->ID );
$category_details = [];
foreach ( $categories as $category ) {
$category_details[] = [
'id' => $category->term_id,
'name' => $category->name,
'slug' => $category->slug,
];
}
$data->data['category_details'] = $category_details;
return $data;
}
add_filter( 'rest_prepare_post', 'add_category_details_to_posts', 10, 3 );
Connect endpoint with Divhunt
- Install the REST API application from the admin panel of your website.
- After installing the REST API application, you can find its icon in the top left corner while you are in the builder. Click on it to open.
- Create a new GET request. For this example, we have named it "WP - All Posts".
Now, when you open the settings of this GET request, do the following:
- In the URL (Endpoint) field, paste the URL that we generated previously.
- Ensure the "Source" option is checked under the settings tab.
- Click the execute icon in the middle of the top bar to verify everything is working correctly. If you see a JSON data preview on the right (as shown in the screenshot), everything is correct, and we are ready to use this data in the builder.
Show data on a website
Apply Source
Now that we have connected our REST API, our builder has access to that data.
- Open the page where you want to display the posts.
- Select a post element inside the builder.
- In the advanced settings on the right sidebar, set the Source to REST API. For this example, we named our API "WP - All Posts".
Show data
Now all the tags inside the post element, where we previously applied the source, have access to the data from the REST API. This means we are now ready to connect titles, images, dates, and other fields. This data is stored in our variables under the name "value". Learn more about how variables work here.
Table of content
Create your website visually – fast and easy.
Build a stunning website with ease using our visual builder. No experience needed – just drag, drop, and launch your site today.
Build and publish websites for free
Build and host your site with a custom domain for free. Upgrade anytime for expanded limits.
Build for free
Built in Divhunt
We use cookies.
By clicking “Accept All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View ourCookie Policyfor more information.
Optahunt
Preferences
Manage your cookie preferences.
Essentials
Always Active
These are required for the website to work, like remembering your privacy settings, keeping you logged in, or enabling secure payments.
Performance
These help us understand how people use our site (like which pages are most popular) so we can make improvements.
Personalization
These allow the site to remember your preferences (such as language or region) and provide enhanced features.
Advertising & Targeting
These are used to deliver ads more relevant to you, limit how often you see them, and measure campaign performance.
Save my preferences
You can always change these settings later via ourCookie Policy pagepage, or via the cookie widget placed at the bottom left of the page.