# How to use the WordPress get\_posts function

The get\_posts function searches your WordPress website and returns posts that match specified criteria. This helps you keep track of your content and better curate articles.

## What is WP get\_posts?

You’ve already got your website up and running and visitors are accessing the content. It seems like most of the work is done, but with every new article, post or subpage you add, it gets harder to keep track of everything. If you use [WordPress](https://www.ionos.ca/digitalguide/hosting/cms/wordpress-examining-the-well-known-cms/), the function get\_posts can be a great help. You can use this to **search and compile posts or pages precisely**. For this purpose, various search criteria are available. Additional [WordPress plugins](https://www.ionos.ca/digitalguide/hosting/blogs/wordpress-plugins/) are not necessary.

Tip Safe, simple, unique. If you buy your [domain name](https://www.ionos.ca/domains/domain-names "Buy domains from IONOS") from IONOS, you get an easy-to-navigate plan and round-the-clock service.

## How does WP get\_posts work?

The WordPress get\_posts function uses search criteria to filter and retrieve **posts, subpages or categories** from your [database](https://www.ionos.ca/digitalguide/hosting/technical-matters/databases/). There are several search criteria options so you can get the content you want without having to further sort it manually. After you define the criteria, get\_posts uses WP\_Query to convert the PHP code into an SQL query. The output is an array in the form of WP\_Posts objects. We’ll explain exactly what this looks like below.

## How and when to use get\_posts on WordPress

WordPress get\_posts is a **powerful search function** that you can use whenever you want to filter and display specific posts. This is not only helpful for you, it also allows you to add value for visitors. You can do this by displaying posts from a specific author, suggesting related posts about a certain topic or listing your most popular articles.

## What is the difference between the WordPress get\_posts and get\_pages functions

The WP functions get\_posts and get\_pages are pretty similar. Both are used to search the database and retrieve posts. However, the main difference is in the **values and names of their parameters**. get\_pages, unlike get\_posts, does not use WP\_Query, but performs the search directly via SQL. get\_pages is also unable to filter posts by the meta\_key and meta\_value parameters.

## get\_posts examples in WordPress

In the following example, we will show you how to use get\_posts in WordPress. First, let’s look at how to perform a simple search query and get the last ten posts of a certain category:

```PHP
<?php
$args = array(
"numberposts" => 10,
"category" => 5
);
$posts_array = get_posts($args);
?>
```

If you want to use the WordPress get\_posts function to display the most popular posts, it works like this:

```PHP
<?php
$args = array(
"numberposts" => 10,
"orderby" => "comment_count"
);
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
?>
```

In this example, you can see that the results are looped using the foreach method from the [MySQL tutorial](https://www.ionos.ca/digitalguide/server/know-how/learn-mysql-in-simple-steps/).

## What are the parameters for get\_posts on WordPress?

get\_posts on WordPress has many different parameters you can use. By defining parameters precisely, you can get search results that accurately reflect your intended query. The most important parameters include the following:

- **exclude**: This parameter allows you to exclude certain search results. The exclusion is done via post ID.
- **meta\_key**: This parameter will only deliver results that have the corresponding key.
- **meta\_value**: Can be specified in addition to meta\_key and specify the value of the key.
- **numberposts**: This parameter specifies how many results will be delivered. If you set it to -1, all results will be displayed. Its default value is 5.
- **order**: Specifies whether the results are displayed in ascending or descending order. Possible values are ASC (ascending) or DESC (descending).
- **orderby**: With “order by”, you can sort the results that get\_posts shows you in WordPress even more precisely. “date” (for sorting by date) and “rand” (for random rendering) are among the most popular.
- **post\_status**: This specifies which posts should be displayed. Possible values include “draft” (for drafts), “publish” (for published posts) or “pending” (for planned publications).

## Summary: get\_posts is a WordPress function with a lot of potential

The get\_posts function is a powerful tool for WordPress users to not only get an overview of their own content but also provide their visitors with better results. The function **offers many possibilities and different parameters** so you can quickly access the content you are looking for.

Tip The world’s most popular CMS is also regularly featured in the IONOS Digital Guide. You can learn how to [add icons in WordPress](https://www.ionos.ca/digitalguide/hosting/blogs/wordpress-icons/) or [debug in WordPress](https://www.ionos.ca/digitalguide/hosting/technical-matters/wordpress-debug/). If you’re looking for [the best WordPress themes](https://www.ionos.ca/digitalguide/hosting/blogs/wordpress-themes-the-best-designs-for-your-website/), you’re in the right place.


This is a markdown version of: [https://www.ionos.ca/digitalguide/hosting/blogs/wordpress-get-posts/](https://www.ionos.ca/digitalguide/hosting/blogs/wordpress-get-posts/) for AI/LLM consumption.