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 every­thing. If you use WordPress, 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. Ad­di­tion­al WordPress plugins are not necessary.

Tip

Safe, simple, unique. If you buy your domain name 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 cat­e­gories from your database. 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 dis­play­ing posts from a specific author, sug­gest­ing related posts about a certain topic or listing your most popular articles.

What is the dif­fer­ence 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 dif­fer­ence is in the values and names of their pa­ra­me­ters. 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 pa­ra­me­ters.

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
$args = array(
"numberposts" => 10,
"category" => 5
);
$posts_array = get_posts($args);
?>
PHP

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

<?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>";
}
?>
PHP

In this example, you can see that the results are looped using the foreach method from the MySQL tutorial.

What are the pa­ra­me­ters for get_posts on WordPress?

get_posts on WordPress has many different pa­ra­me­ters you can use. By defining pa­ra­me­ters precisely, you can get search results that ac­cu­rate­ly reflect your intended query. The most important pa­ra­me­ters 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 cor­re­spond­ing key.
  • meta_value: Can be specified in addition to meta_key and specify the value of the key.
  • num­ber­posts: 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 de­scend­ing order. Possible values are ASC (ascending) or DESC (de­scend­ing).
  • 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 pub­li­ca­tions).

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 pos­si­bil­i­ties and different pa­ra­me­ters 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 or debug in WordPress. If you’re looking for the best WordPress themes, you’re in the right place.

Go to Main Menu