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, 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 are not necessary.

Tip

Safe, simple, unique. If you buy your domain name 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. 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. 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
$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 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 or carry out a WordPress debug. If you’re looking for the best WordPress themes, you’re in the right place.

Was this article helpful?
Page top