wp_query() vs. get_pages() vs. get_posts()
In my last article I discussed custom WordPress queries and what makes them useful in your custom WordPress themes. If you are not familiar with custom WordPress queries, you may want to refer back to my previous article “WordPress Custom Post Queries“. In this article we will be going over the differences and functionality of the wp_query() class, the get_pages() function and the get_posts() function. Each method for querying assets from the wp_post database table comes with its own unique attributes, but all of these commands can be used to define the scope of a query. There are various parameters, which you can set for each of your custom queries to get the desired output on the front-end. This is the main benefit of a custom query. To see each method’s accepted parameters, make sure to check out the WordPress documentation for each class and function.
WP_Query is a class, which can be used to query or request data from the wp_post database table. The class accepts various parameters to give the ability to define a scope and output for the array of assets you wish to search for. In a way similar to the default WordPress query, to display the requested data on the front end of the website, you would use wp_query’s methods and properties, such as the_post() or is_post()
When displaying your queried assets on the front end, you will use what is known as a WordPress loop. Utilizing the while() loop function, you can siphon through the queried assets, using the wp_query’s methods to access each assets data. The_post() method gives you the ability to use functions such as the_title() and the_content() .
You can use a loop multiple times on one page, however before proceeding with multiple loops for any query using wp_query(), make sure to reset the post data using the wp_reset_postdata() function. This function restores the main query loop after a secondary query loop using wp_query is used.
The get_posts() function uses the wp_query class, which makes the wp_query class the grandfather of all WordPress query functionalities. If you want to query assets from your wp_post database table, then I first recommend using wp_query. It requires fewer lines of code and get’s the job done more efficiently.
Get_Posts() uses the wp_query class to return an array of post objects. Unlike the get_pages() function, the get_posts() function is not limited by post_type settings. In fact, you could just use the get_posts() function in place of the get_pages() function, but you could also use wp_query() in place of get_posts(). The difference between wp_query() and get_posts() is basically the type of loop you want to use to display the posts on the front end. So in the end it mainly boils down to your preference and comfort.
Similar to get_pages(), to display queried assets from the get_posts() function, you would use a foreach loop to access each post object in the array. A unique aspect of get_posts is that, you can opt in to use the wp_query methods and properties using the setup_postdata() function for each loop iteration. This makes it slightly easier and more efficient to output asset data on the front-end than get_pages().
Because the get_posts() function uses the wp_query class to query post, to have multiple get_posts() loops you will need to use the wp_reset_postdata() function after each loop.
Get_Pages() is a function, which queries the database directly and returns an array of page objects. The function is limited to post_types which are set as hierarchical, meaning you can organize the pages by parent pages and child pages. This allows you to specify parameters for the function relating to hierarchical posts. This is the main benefit of the get_pages() function. However, if you try to query a post_type without the hierarchical setting set to true, the get_pages() function will return false. The parameters that get_pages() accepts are still very similar to the get_posts() function, but the parameters names and default values differ do differ. Make sure to check the get_posts and get_pages WordPress documentation for specific differences in parameter values and names.
Similar to get_posts(), the get_pages() function returns an array of objects. To display and access the data associated with the returned page objects, you use foreach loop to iterate through each object in the array. On each foreach iteration you can access each page object’s value using index names like $page_object->ID. To see what is available within a page object you can use the print_r() php function, to display the full array returned by the get_pages() function. This is also a good method to test your custom page queries and make sure you are querying the correct pages or posts defined by your parameters.