# 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?

Once your website is up and online, visitors are accessing the content and you’re gradually adding more content, a lot of the work is done. However, the more articles, posts or subpages you add, the harder it becomes to keep track of them. If you use [WordPress](https://www.ionos.co.uk/digitalguide/hosting/cms/wordpress-examining-the-well-known-cms/), the function get\_posts is 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.co.uk/digitalguide/hosting/blogs/wordpress-plugins/) are not necessary.

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

## How does WP get\_posts work?

The WordPress get\_posts function accesses your **posts, subpages or categories** to fit your search criteria and retrieves the data. Subpages or categories from the [database](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/databases/). You can formulate these in such a way that you get the desired results, without the need for additional manual subdivision. get\_posts then 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 helpful for you, but also allows you to add value for visitors. So, you can display more posts by a specific author, play out more posts on a topic, or list your most popular articles.

Tip The smartest solution is [WordPress Pro](https://www.ionos.co.uk/hosting/wordpress-pro "WordPress Pro by IONOS"). You get all the required features for your project. Create your online presence quickly and benefit from regular backups and personal support!

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

Basically, 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 very easily. The first step we 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.co.uk/digitalguide/server/know-how/learn-mysql-in-simple-steps/).

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

There are many different parameters through which you can refine your query with get\_posts on WordPress. The more precise you use these parameters, the more suitable the output will be. 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 get an overview of their own content on the one hand and on the other hand to give visitors even better results. The function **offers many possibilities** and provides accurate results through different parameters.

Tip The world’s most popular CMS also features regularly in the IONOS Digital Guide. You can learn how to add [icons in WordPress](https://www.ionos.co.uk/digitalguide/hosting/blogs/wordpress-icons/) or [carry out a WordPress debug](https://www.ionos.co.uk/digitalguide/hosting/technical-matters/wordpress-debug/). If you’re looking for [the best WordPress themes](https://www.ionos.co.uk/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.co.uk/digitalguide/hosting/blogs/wordpress-get-posts/](https://www.ionos.co.uk/digitalguide/hosting/blogs/wordpress-get-posts/) for AI/LLM consumption.