Table of contents
Get the industry’s fastest WordPress hosting ◦ 99.999% uptime
◦ Comprehensive security
◦ 24/7 support

WordPress GuideDevelopment → 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:

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:

Best practices

When using get_posts, keep the following best practices in mind:

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

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!

Trust us to help you choose the ideal hosting solution

Loading form…