Home >CMS Tutorial >WordPress >Exploring the WordPress get_posts Function
Many WordPress plugins retrieve posts from the database by customizing the sorting order, retrieving posts based on a specific meta key or taxonomy. Have you ever wondered how these plugins retrieve customized lists of posts without writing any SQL queries? In this tutorial we’ll learn how to do exactly that.
In this article we’ll explore the get_posts() function with some examples of how to use it in your own projects. We’ll also cover some typical use cases for this function and how it’s different from the WP_Query object and get_pages function.
The get_posts function has been available in WordPress core since WordPress 1.2.0. This function is basically used to retrieve posts from the database by applying custom filters and sorting the final result based on a set of parameters.
The get_posts() function returns an array of WP_Post objects. Each WP_Post object represents an individual post.
Internally get_posts uses the WP_Query object to construct and execute the SQL queries based on the passed set of parameters.
Note: Posts means post, page and custom post type.
Plugins use get_posts function instead of WP_Query object because using the WP_Query object directly alters the main loop (i.e., the global $wp_query variable) which would cause site issues.
Both of them are used to retrieve posts from the WordPress database, however, here are some of the differences between them:
The get_posts function takes only one argument as an array. The array contains the different parameters to apply custom filters and sort the result.
Here’s a code example which shows how to call this function and the various parameters available:
<span><span><?php </span></span><span> </span><span><span>$args = array( </span></span><span> <span>"posts_per_page" => 5, </span></span><span> <span>"paged" => 1 </span></span><span> <span>"tax_query" => array( </span></span><span> <span>array( </span></span><span> <span>"taxonomy" => "category", </span></span><span> <span>"field" => "slug", </span></span><span> <span>"terms" => "videos,movies", </span></span><span> <span>) </span></span><span> <span>), </span></span><span> <span>"orderby" => "post_date", </span></span><span> <span>"order" => "DESC", </span></span><span> <span>"exclude" => "1123, 4456", </span></span><span> <span>"meta_key" => "", </span></span><span> <span>"meta_value" => "", </span></span><span> <span>"post_type" => "post", </span></span><span> <span>"post_status" => "publish" </span></span><span><span>); </span></span><span> </span><span><span>$posts_array = get_posts($args); </span></span><span> </span><span><span>?></span></span>
There are more parameters available, but these are the most commonly used ones. Let’s look at each of these parameters:
The get_posts function returns an array that contains WP_Post objects. Here are the important properties of the WP_Post object:
Let’s check out some examples using the get_posts function.
If you want to display the top n number of the most discussed posts on your site, then you can use get_posts to retrieve them. Here’s an example:
<span><span><?php </span></span><span> </span><span><span>$args = array( </span></span><span> <span>"posts_per_page" => 5, </span></span><span> <span>"paged" => 1 </span></span><span> <span>"tax_query" => array( </span></span><span> <span>array( </span></span><span> <span>"taxonomy" => "category", </span></span><span> <span>"field" => "slug", </span></span><span> <span>"terms" => "videos,movies", </span></span><span> <span>) </span></span><span> <span>), </span></span><span> <span>"orderby" => "post_date", </span></span><span> <span>"order" => "DESC", </span></span><span> <span>"exclude" => "1123, 4456", </span></span><span> <span>"meta_key" => "", </span></span><span> <span>"meta_value" => "", </span></span><span> <span>"post_type" => "post", </span></span><span> <span>"post_status" => "publish" </span></span><span><span>); </span></span><span> </span><span><span>$posts_array = get_posts($args); </span></span><span> </span><span><span>?></span></span>
Here, we are using the orderby parameter to sort the posts based on the number of comments, retrieving the top 10 posts.
You can also easily retrieve random posts. This is helpful to recommend users another article on your site once they’ve finished reading the current one. Here’s the code for this:
<span><span><?php </span></span><span> <span>$args = array("posts_per_page" => 10, "orderby" => "comment_count"); </span></span><span> <span>$posts_array = get_posts($args); </span></span><span> <span>foreach($posts_array as $post) </span></span><span> <span>{ </span></span><span> <span>echo "<h1>" . $post->post_title . "</h1><br>"; </span></span><span> <span>echo "<p>" . $post->post_content . "</p><br>"; </span></span><span> <span>} </span></span><span><span>?></span></span>
In the above example, we passed the value rand to the order_by parameter.
We might want to retrieve all posts which have a particular meta key and value assigned. For example: some blogs have a reviewer for every article. We might want to retrieve articles reviewed by a particular reviewer.
Here is the code to do just that:
<span><span><?php </span></span><span> <span>$args = array("posts_per_page" => 1, "orderby" => "rand"); </span></span><span> <span>$posts_array = get_posts($args); </span></span><span> <span>foreach($posts_array as $post) </span></span><span> <span>{ </span></span><span> <span>echo "<h1>" . $post->post_title . "</h1><br>"; </span></span><span> <span>echo "<p>" . $post->post_content . "</p><br>"; </span></span><span> <span>} </span></span><span><span>?></span></span>
Here, we’re retrieving all the posts reviewed by “narayanprusty”. We’re assuming the reviewer name is stored via the meta key “reviewer” for every post.
We may want to retrieve posts of a custom post type with a custom taxonomy name. Consider this code example:
<span><span><?php </span></span><span> <span>$args = array("posts_per_page" => -1, "meta_key" => "reviewer", "meta_value" = "narayanprusty"); </span></span><span> <span>$posts_array = get_posts($args); </span></span><span> <span>foreach($posts_array as $post) </span></span><span> <span>{ </span></span><span> <span>echo "<h1>" . $post->post_title . "</h1><br>"; </span></span><span> <span>echo "<p>" . $post->post_content . "</p><br>"; </span></span><span> <span>} </span></span><span><span>?></span></span>
In this example, we’re retrieving the posts of a custom post type named “coupons” which belong to the “plugins” and “themes” custom taxonomies.
In this article we saw how the get_posts function works, the various parameters it supports, looping through the returned result and some sample use cases. The get_posts function is one of the most used WordPress functions, I hope you can now start using it your own projects.
Both get_posts and WP_Query are used to retrieve posts from your WordPress database. However, they differ in their usage and flexibility. get_posts is a simpler function, ideal for beginners and for situations where you only need to retrieve a specific set of posts. On the other hand, WP_Query is more powerful and flexible. It allows for more complex queries and gives you more control over the WordPress loop. While get_posts is easier to use, WP_Query provides more advanced features for customizing your queries.
You can control the number of posts returned by get_posts by using the ‘numberposts’ parameter. By default, it is set to 5. If you want to retrieve all posts, you can set ‘numberposts’ to -1. For example, to get 10 posts, you would use: get_posts(array(‘numberposts’ => 10));
Yes, you can use get_posts to retrieve custom post types. You just need to specify the ‘post_type’ parameter in your query. For example, if you have a custom post type called ‘products’, you can retrieve these posts with: get_posts(array(‘post_type’ => ‘products’));
You can sort the posts returned by get_posts by using the ‘orderby’ and ‘order’ parameters. ‘orderby’ determines the field to sort by, and ‘order’ determines the sorting order (ASC for ascending and DESC for descending). For example, to sort posts by title in ascending order, you would use: get_posts(array(‘orderby’ => ‘title’, ‘order’ => ‘ASC’));
Yes, you can use get_posts to retrieve posts from a specific category. You just need to specify the ‘category’ parameter in your query. For example, to get posts from the category with the ID 3, you would use: get_posts(array(‘category’ => 3));
To retrieve posts from multiple categories, you can pass an array of category IDs to the ‘category’ parameter. For example, to get posts from the categories with the IDs 3 and 4, you would use: get_posts(array(‘category’ => array(3, 4)));
Yes, you can use get_posts to retrieve posts by a specific author. You just need to specify the ‘author’ parameter in your query. For example, to get posts by the author with the ID 1, you would use: get_posts(array(‘author’ => 1));
To retrieve posts that contain a specific meta value, you can use the ‘meta_key’ and ‘meta_value’ parameters. For example, to get posts that have a meta key of ‘color’ and a meta value of ‘blue’, you would use: get_posts(array(‘meta_key’ => ‘color’, ‘meta_value’ => ‘blue’));
Yes, you can use get_posts to retrieve posts that have a specific tag. You just need to specify the ‘tag’ parameter in your query. For example, to get posts that have the tag ‘WordPress’, you would use: get_posts(array(‘tag’ => ‘WordPress’));
To retrieve posts from a specific date range, you can use the ‘date_query’ parameter. This parameter accepts an array of arrays, with each inner array defining a date query clause. For example, to get posts from January 2020, you would use: get_posts(array(‘date_query’ => array(array(‘year’ => 2020, ‘month’ => 1))));
The above is the detailed content of Exploring the WordPress get_posts Function. For more information, please follow other related articles on the PHP Chinese website!