get_posts crawls your WordPress site and provides you with posts that match defined criteria. This allows you to keep an overview of your content and can better manage the distribution of articles.
What is the WordPress get_posts function?¶
Once your website is online, you can post articles that people will be happy to read and comment on. But the more articles, posts or subpages there are, the more difficult it is to keep an overview of the content on your site. If you use WordPress the get_posts function will be of great help to you. This allows you to search and collect articles or pages in a targeted manner. You have different search criteria for this. There is no need to install additional WordPress plugins.
How does WP get_posts work?
The get_posts function of WordPress uses your search criteria to filter certain articles, subpages or categories of the database. You can formulate them so that only the results you want are displayed and no manual subdivision is needed. get_posts then uses WP_Query to transform the PHP code into an SQL query. The result is an array (or array) in the form of WP_Posts objects. We explain to you below what exactly this looks like.
How and when to use WordPress get_posts?¶
In WordPress, get_posts is a powerful search function which you can use whenever you want to filter and display certain posts. This function is useful for you as well as for your visitors since it allows you to create added value. You can view contributions from a specific author, read articles on a given topic, or list your most popular articles.
What is the difference between get_posts and get_pages WordPress functions?¶
In principle, WP’s get_pages and get_posts functions are similar. Both are used to search the database and to display articles. However, the values and names of their parameters differ. Unlike get_posts, get_pages does not use WP_Query and performs the search directly through SQL. Also, get_pages cannot filter articles based on meta_key and meta_value parameters.
WordPress get_posts: examples¶
Here’s how to easily use get_posts in WordPress. We can start by performing a simple search requesting that the last ten articles of a certain category be displayed:
<?php
$args = array(
"numberposts" => 10,
"category" => 5
);
$posts_array = get_posts($args);
?>
PHP
Here’s how you can use WordPress’ get_posts function to display the most popular posts, for example:
<?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 see the output results are looped with the foreach method, which you know from the MySQL tutorial, and then displayed.
What are the parameters for get_posts in WordPress?¶
There are many different parameters that allow you to fine-tune your query with get_posts on WordPress. The more you use these settings, the better the result will be. The most important parameters are:
- exclude : This setting allows you to exclude certain search results. Exclusion is done via the article ID.
- meta_key : this parameter allows you to display only the results that have the corresponding key.
- meta_value : can be specified in addition to meta_key to specify the key value.
- numberposts : this parameter indicates the number of results to provide. If you set it to -1, all results will be displayed. Its default value is 5.
- order : indicates whether the results are displayed in ascending or descending order. Possible values are ASC (ascending) or DESC (descending).
- orderby : allows you to sort the results that get_posts displays in WordPress even more precisely. There are many different values, the most common being “date” (for sorting by date) and “rand” (for random presentation).
- post_status : This parameter allows you to specify which types of messages should be displayed. Possible values are for example “draft” (for drafts), “publish” (for published articles) or “pending” (for planned publications).
In summary: get_posts is a WordPress feature with great potential¶
For WordPress users, get_posts is a powerful tool that allows them to have an overview of their own content and offer visitors even more relevant results. Function offers many possibilities and provides precise results thanks to the different parameters.