query

WP_Query

How To Create WordPress Custom Queries with WP_Query?

WordPress custom queries are very powerful tool. If you want to display on your index.php or single.php file other content different from what the WordPress shows by default, just create WordPress custom queries using WP_Query. For those that don’t know, WP_Query is a class that allows you to create WordPress custom queries and retrieve your posts according to criteria defined via the parameter to the constructor of this class.

To use this application you must store it in a variable and use that object in WordPress loop. In this tutorial I will show you some great examples on how to use WordPress custom queries or WP_Query in your own WordPress projects.

WordPress Custom Queries Examples
This class can be configured as desired, and there are many possibilities available to you, here are some of WordPress custom queries examples I find most useful:

Example 1: Select all posts

$query = new WP_Query(array(‘post_type’ => ‘post’));

Example 2: Select all posts and all pages