phpwordpress

Difference between get_pages() and get_posts() functions in WordPress


I am bit confused about above two. However, I am aware about the nature of both the functions.

1. get_pages() - fetches all pages from db.
2. get_posts() - fetches all posts from db.

But, my question is, when to use these functions? Do both of them return all the pages/posts which we can get through WP_Query() or do they both work in different ways?


Solution

  • With get_pages(), you can set the type of post you are retrieving in the post_type parameter of the arguments. It returns an array of page objects that you can iterate over to display the contents. get_posts() is used when you want an array of post objects returned. This enables the use of specialized methods and properties when iterating over the returned array.

    More about the difference can be found here.