Creating a custom query in WordPress is needed for your website as you start building your own custom theme or plugin or if you are working with template by creating methods and using page and post parameters and create SQL queries as well.
When you create custom query in WordPress you will be using parameters to define what content will be returned in your Loop, choice between your loop which you want to define and a custom Loop or fluctuating or altering the main Loop or primary loop.
When creating Loops it’s essential to understand what parameters are available to help define what content will be displayed or what you want to display. You can use many different, sometimes confusing, parameters in creating your custom Loop to alter the output of your content.
Sometimes you can use single parameter or multiple parameters can also be set per query by separating the parameter name and values with an ampersand. For a detailed list of available parameters, you can visit here for more in details regarding wp_query parameter.
The most noticeable and sometimes most maximum and used parameters, select the number and types of posts to be displayed:
which Loads an individual post by ID.
which Loads post based on post slug which is permalink tail.
Loads posts by post status. For example, if you choose to see only drafts, use post_status=draft.
Excludes sticky posts from being returned first. A sticky post is one that always sorts to the top of the list of posts when scrolling the page, independent of the other parameters set for the query. You can have multiple sticky posts on one page or post, making them useful for calling attention to news announcements, highlighting changes, or otherwise grabbing reader’s attention, and this parameter lets you drop them from their priority slot at the top of the list.
which loads posts based on type. If you only want to look at pages, not posts, post_type=page
will retrieve them. This parameter enables special-purpose loops to select content based on custom post types, as you’ll see in Chapter 7.
Number of posts to load per page. This is the default. To show all posts, set this parameter to –1.
Number of posts to skip before loading.
Working with custom query of page Parameters, Pages have parameters similar and comparable to those for posts to control their selection
which loads an individual page by ID. Like post IDs and user IDs, page IDs can be found in the dashboard by hovering over a page and looking at the URL displayed at the bottom on your browser.
which loads the page by name, in this case the Contact page.
which loads a child page by slug, or hierarchy of slugs (that is, its path).
Posts can also be organized or sorted by category into which they were placed, by tags applied to the post, or by author information:
Load posts based on category ID.</ p>
Loads posts based on category name. Note that if a post belongs to more than one category, it will show up in selections for each of those categories.
which Loads posts based on tag name.
which loads posts based on tag ID.
which loads posts based on author ID.
Loads posts based on author’s name.
Related Categories:
1. How to Use WP_Query Object in WordPress with WP Query Custom Loops
2. Template Tags in WordPress and Uses
3. WordPress Debug log: Enable in wp-config.php, SET to True & SET Log path file
4. Learn Everything About wp-config.php File WordPress
5. What are Permalinks in WordPress: How to Enable, Customize Permalink Tags
Parameters to select content based while building custom template for your website and you can also modify and change the sort parameter and the sort order as well. If you’re building an online index, and want to show an alphabetical post listing, you’ll set the parameters for querying posts by month and author, but order the results by title.
which loads posts created in December.
which Loads posts created on the 12th day of the month.
which loads posts created in 2012.
which field to order posts by.
which defines ascending or descending order of orderby.
which loads posts by custom field name which refer to the custom taxonomy and how custom fields are added to posts.
which loads posts by custom field value. Must be used in conjunction with the meta_key parameter above.
Below is the following examples use the $myPosts>query() function
from the $myPosts
custom query object created in the example to select the content displayed in your custom Loop.
$myPosts = new WP_Query( 'p=1' );
With the below custom query, which will avoid skipping the first post.
$myPosts = new WP_Query( 'posts_per_page=5&offset=1' );
Check Image below:
$today = getdate(); // get todays date
$myPosts = new WP_Query('year=' .$today["year"]
.'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
$myPosts = new WP_Query( 'monthnum=10&day=31&year=2013' );
$myPosts = new WP_Query( 'cat=5&tag=bacon' );
$myPosts = new WP_Query( 'cat=-5&tag=bacon' );
$myPosts = new WP_Query( 'tag=writing,reading' );
$myPosts = new WP_Query( 'tag=writing+reading+tv' );
$myPosts = new WP_Query( 'meta_key=color&meta_value=blue' );