WP_Query in WordPress queries and displays posts based on specific parameters. It allows developers to customize how content is retrieved and displayed, offering a more flexible alternative to the default loop.
What is WP_Query?
WP_Query is primarily used for:
1) Filtering posts based on custom criteria (categories, tags, custom post types, etc.).
2) Creating custom loops to display content in specific layouts.
Basic Example of WP_Query
Here’s a simple example where we retrieve the latest five blog posts:
<?php $args = array( 'post_type' => 'post', // Fetch posts (you can also use 'page' or custom post types) 'posts_per_page' => 5, // Limit to 5 posts 'orderby' => 'date', // Order by post date 'order' => 'DESC' // Show most recent first ); $query = new WP_Query($args); if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); the_title('<h2>', '</h2>'); // Display post title the_excerpt(); // Display excerpt endwhile; wp_reset_postdata(); // Reset query data else: echo 'No posts found'; endif; ?>
Example Use Cases for WP_Query
1. Query Posts by Category
You can retrieve posts from a specific category using category_name. The following example pulls posts from the “news” category:
<?php $args = array( 'category_name' => 'news', // Fetch posts from 'news' category 'posts_per_page' => 3, // Limit to 3 posts ); $query = new WP_Query($args); ?>
2. Query Custom Post Types
WP_Query allows you to work with custom post types. In this example, we retrieve four portfolio items:
<?php $args = array( 'post_type' => 'portfolio', // Custom post type: 'portfolio' 'posts_per_page' => 4, // Limit to 4 items ); $query = new WP_Query($args); ?>
3. Meta Query (Custom Fields)
You can filter posts based on custom fields (meta data). Here’s an example that queries products with a custom field price greater than 100:
<?php $args = array( 'post_type' => 'product', // Custom post type: 'product' 'meta_query' => array( // Meta query to filter by custom field 'price' array( 'key' => 'price', 'value' => 100, 'compare' => '>', 'type' => 'NUMERIC' ) ) ); $query = new WP_Query($args); ?>
Pagination with WP_Query
WP_Query supports pagination using the paged parameter. You can use it to paginate your content like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'post', 'posts_per_page' => 5, 'paged' => $paged ); $query = new WP_Query($args); if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); the_title('<h2>', '</h2>'); endwhile; previous_posts_link('Previous'); // Display link to previous page next_posts_link('Next', $query->max_num_pages); // Display link to next page wp_reset_postdata(); else: echo 'No posts found'; endif; ?>
Conclusion:
WP_Query is a versatile tool in WordPress that customizes how posts are retrieved and displayed. Whether you want to filter posts by category, custom fields, or post
types, WP_Query provides endless possibilities for building dynamic content layouts.
This class is indispensable for WordPress developers looking to extend the functionality of WordPress themes and plugins.
Read Also:-
How To Create custom post type (CPT) in WordPress
How to Create a Child Theme in WordPress
Also Visit :-