Home » Functions » get_posts

The get_posts function is a powerful WordPress function that retrieves a list of posts or pages based on a variety of criteria. It can be used to display a list of posts on a page, to create a custom post archive, or to generate a feed of posts for syndication.

The function takes an array of arguments that determine which posts are retrieved and how they are sorted. These arguments include things like post type, category, tag, author, date range, and more. By using different combinations of these arguments, you can create highly customized lists of posts that suit your needs.

Example Usage Code:

To get a list of all published posts:

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,

$posts = get_posts( $args );

foreach ( $posts as $post ) {
    setup_postdata( $post );

This code retrieves all published posts and displays their titles. The posts_per_page argument is set to -1, which means that all posts will be retrieved. This is useful if you want to get a complete list of all posts, rather than just a limited set.

Learn More on

Register an account to save your snippets or go Pro to get more features.