Arin Adamson
By Arin Adamson | Web Dev | June 15, 2015

WordPress Custom Post Queries

wordpress-e1406668251484 What is a post query? A query, speaking in web development terms, is a request from a data source. In the case of WordPress, a post query is a request from the database requesting post objects or posts, along with all of their data. Out of the box, WordPress contains all the default functionality that is needed to make requests to the database and display associated posts on any particular page. What if you wanted to modify the default WordPress post query to restrict or expand the scope of the posts displayed on a page? This is fairly easy with the use of a custom post query. Using a specific set of parameters, we can display posts in various ways, which can be handy when working on those custom WordPress development projects.

WordPress’ default post query requests posts depending on which page you are on. If you are on the home blog page, it will query all blog posts. If you are on a single post page, it will query only the single post. If you are on a category page, it will query the posts assigned to that category. What if you wanted to show all of your pages or another post type on your blog page? You would need to create a custom WordPress query which included additional post types in the parameters sent to the wp_query() class. What about displaying posts only assigned to a specific taxonomy? This also requires a custom WordPress query with a tax_query parameter for the wp_query() class. There are many instances where you may require a custom post query, but what if you are asking yourself ‘do I need to use a custom post query for my project?’ Typically the answer is no, but if you are going to be required to display content from pages, posts or custom post types in different areas of the website, which would otherwise not be possible, you’re going to need the wp_query() class.

wp_query() is not the only method WordPress has for querying posts. It is the most efficient method and least restrictive for querying the WordPress content database, but you can also use query_posts(), get_posts and get_pages().  If you wanted to simply query one page or post you can use the get_post() or get_page() functions, which uses the page/post ID as a parameter and returns an object. However, querying multiple posts is basically the same with get_posts(), get_pages and wp_query(). The query_posts() function is not recommended for use, because it re-runs SQL queries and sometimes simply refuses to function. It’s much better practice to use the get_posts(), get_pages() functions and wp_query() class, which are safe to use any where on your website. As long as you use those three methods to query posts. It is not important which you use. They all accept the same parameters and allow you to basically do the same tasks with minor differences. However, the parameters that you pass are important!

Without parameters, WordPress would not know what posts your are trying to request from the database. Between filtering the posts by post types, taxonomies, categories, tags, post/page ID, post/page page parent ID and more, you have plenty of ways to get the content you want to query with the use of the wp_query() parameters. You can view a full lists of accepted parameters and examples using the wp_query() wordpress documentation. The get_posts() and get_pages() functions are different from the wp_query() class in how you implement them into your website. The get_posts() and get_pages() functions return and array, which means you would not use the standard loop method. Instead you would use a foreach. So there are differences, but you should be able to return the same post data in both the custom post query functions and classes. In my next article I will go over the differences of their implementations and provide some examples of custom WordPress post queries.

background dots

In the time it takes to read this sentence, you could be on your way to a well-oiled demand generation machine. Ready for your blueprint?

yes, i want my Digital blueprint

Notice: Trying to access array offset on value of type null in /nas/content/live/digitalreach/wp-content/themes/DRAmatic/single.php on line 152

Notice: Trying to access array offset on value of type null in /nas/content/live/digitalreach/wp-content/themes/DRAmatic/single.php on line 153

Notice: Trying to access array offset on value of type null in /nas/content/live/digitalreach/wp-content/themes/DRAmatic/single.php on line 156