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.
What is the get_posts()
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.
Continue reading %Exploring the WordPress get_posts Function%
by Narayan Prusty via SitePoint
No comments:
Post a Comment