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, 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 are not necessary.
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 categories 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 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
$args = array(
"numberposts" => 10,
"category" => 5
);
$posts_array = get_posts($args);
?>
PHPIf 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>";
}
?>
PHPIn 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?
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.
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.