◦ Comprehensive security
◦ 24/7 support
WordPress Guide → Development → get_posts
WordPress get_posts and how to use it (with examples)
The get_posts function in WordPress is a versatile tool that allows developers to retrieve posts, pages, and custom post types based on specific criteria. This function is particularly useful when you need to display a customized list of content without altering the main query.
Get fast, reliable hosting for WordPress
Power your site with the industry’s fastest, most optimized WordPress hosting
How get_posts works
At its core, get_posts utilizes the WP_Query class to fetch data from the WordPress database. By passing an array of parameters to get_posts, you can filter and sort posts to match your desired criteria. The function then returns an array of WP_Post objects, each representing an individual post. citeturn0search0
Common parameters for get_posts
To effectively use get_posts, it’s essential to understand its parameters:
- numberposts: Specifies the number of posts to retrieve. The default is 5.
- category: Filters posts by category ID or a comma-separated list of IDs.
- include: An array of post IDs to include in the results.
- exclude: An array of post IDs to exclude from the results.
- post_type: Defines the type of content to retrieve, such as ‘post’, ‘page’, or any custom post type.
- post_status: Retrieves posts based on their status, like ‘publish’, ‘draft’, or ‘private’.
- orderby: Determines the field to sort the results by, such as ‘date’, ‘title’, or ‘rand’ for random order.
- order: Sets the sorting direction, either ‘ASC’ for ascending or ‘DESC’ for descending.
For a comprehensive list of parameters, refer to the WordPress Developer Reference.
Using get_posts: A basic example
Here’s a straightforward example of how to use get_posts to retrieve the latest 10 published posts:
$args = array(
‘numberposts’ => 10,
‘post_status’ => ‘publish’,
);
$recent_posts = get_posts($args);
foreach ($recent_posts as $post) {
setup_postdata($post);
echo ‘<h2>’ . get_the_title() . ‘</h2>’;
echo ‘<p>’ . get_the_excerpt() . ‘</p>’;
}
wp_reset_postdata();
This script fetches the 10 most recent published posts and displays their titles and excerpts. The setup_postdata($post) function sets up global post data, allowing template tags like get_the_title() and get_the_excerpt() to work seamlessly. After the loop, it’s crucial to call wp_reset_postdata() to restore the global $post variable to its original state.
Advanced usage scenarios
Beyond basic retrievals, get_posts can handle more complex queries:
Retrieving posts by a specific author
To get posts from a particular author, use the ‘author’ parameter with the author’s ID:
$args = array(
‘author’ => 1, // Replace with the desired author ID
‘post_status’ => ‘publish’,
);
$author_posts = get_posts($args);
Fetching posts within a date range
To retrieve posts published within a specific timeframe, utilize the ‘date_query’ parameter:
$args = array(
‘date_query’ => array(
array(
‘after’ => ‘January 1, 2023’,
‘before’ => ‘December 31, 2023’,
‘inclusive’ => true,
),
),
‘post_status’ => ‘publish’,
);
$date_posts = get_posts($args);
Retrieving custom post types
To fetch posts from a custom post type, specify the ‘post_type’ parameter:
$args = array(
‘post_type’ => ‘product’, // Replace with your custom post type
‘post_status’ => ‘publish’,
);
$custom_posts = get_posts($args);
get_posts vs. WP_Query
While both get_posts and WP_Query are used to retrieve posts, they have distinct differences:
- get_posts: Returns an array of posts and doesn’t modify the global $wp_query object. It’s ideal for secondary loops where you don’t want to interfere with the main query.
- WP_Query: Provides a more robust set of functionalities and is better suited for complex queries. However, it can alter the main query if not used carefully.
Best practices
When using get_posts, keep the following best practices in mind:
- limit the number of posts retrieved: Fetching a large number of posts can impact performance. Always specify the ‘numberposts’ parameter to control the number of posts returned.
- use caching: Implement caching mechanisms to store query results, reducing the load on the database for repeated queries.
- reset post data: After running a custom query, always use wp_reset_postdata() to ensure subsequent queries or template tags function correctly.
Upgrade your WordPress experience
Ready to take the training wheels off your WordPress site? Upgrade to professional hosting and see how better speeds, security, and reliability provide a foundation for a website and a brand that people find engaging and trustworthy. Liquid Web’s WordPress hosting options configure business-class servers and support plans specifically for WordPress websites.
Don’t want to deal with server management and maintenance? Our fully managed hosting for WordPress is the best in the industry. Our team are not only server IT experts, but WordPress hosting experts as well. Your server couldn’t be in better hands.
Click through below to explore all of our WordPress hosting options, or chat with a WordPress expert right now to get answers and advice.
Additional resources
What is managed WordPress hosting? →
Get details and decide if managed WordPress hosting is right for you.
What is the WordPress loop? →
Learn how the WordPress Loop works to display posts dynamically on your website.
A complete guide to WordPress shortcodes →
Shortcodes make life easier. Learn how to get started!