AI Blog Monetization
 
WP_Query Deep Dive: The Ultimate Guide to Filtering WordPress Posts Your Way!

WP_Query Deep Dive: The Ultimate Guide to Filtering WordPress Posts Your Way!

WP_Query in WordPress is a powerful class that enables you to perform custom queries to retrieve specific posts.

By specifying $args (arguments), you can retrieve posts based on a variety of conditions such as post type, category, tag, and custom fields. Below is a list of the main $args parameters that can be passed to WP_Query. These representative query parameters allow you to precisely define the retrieval criteria.


🔷 Basic Parameters

Parameter Description Example
post_type Post type 'post', 'page', custom post types
posts_per_page Number of posts to retrieve 4, -1 (all)
offset Number of posts to skip 10 (starts from 11th post)
paged Page number (for pagination) 2 (page 2)
post_status Post status 'publish', 'draft', 'private'
orderby Sort criteria 'date', 'title', 'modified', 'rand', 'meta_value'
order Ascending or descending 'ASC', 'DESC'

🔷 Post ID / Slug Parameters

Parameter Description Example
p Specific post ID 23
name Post slug 'my-post'
post__in Array of specific post IDs (include only) [1, 5, 9]
post__not_in Exclude specific post IDs [7, 8]

🔷 Category & Taxonomy Parameters

Parameter Description Example
cat Category ID 3
category_name Category slug 'news'
category__in Include multiple category IDs [3, 5]
category__not_in Exclude category IDs [1, 6]
tag Tag slug 'featured'
tag__in Array of tag IDs [2, 3]
tax_query Custom taxonomy query array([...]) (see below)

🔷 Custom Fields (Meta) Parameters

Parameter Description Example
meta_key Meta key name 'price'
meta_value Meta value '100'
meta_compare Comparison operator '=', '!=', '>', '<', 'LIKE'
meta_query Combine multiple conditions array([...]) (for complex searches)

🔷 Date Parameters

Parameter Description Example
year Year 2025
monthnum Month 6
day Day 20
date_query Advanced date conditions array([...])

🔷 Others

Parameter Description Example
s Keyword search 'WordPress'
author Author ID 1
author__in Array of author IDs [1, 2]
author_name Author slug 'admin'
ignore_sticky_posts Ignore sticky posts true
post_parent Parent post ID 25 (for hierarchical pages, etc.)

🔶 tax_query Example (Custom Taxonomy)

'tax_query' => array(
    array(
        'taxonomy' => 'genre',
        'field'    => 'slug',
        'terms'    => array( 'drama', 'comedy' ),
        'operator' => 'IN',
    ),
),

✅ Explanation of Your Example

$args = array(
    'posts_per_page' => 4, // latest 4 posts
    'post_type' => 'post', // standard posts
    'orderby' => 'date',   // order by post date
    'order' => 'DESC',     // descending (newest first)
    'category__in' => get_term_children($parent_id, 'category'), // child categories of a specific parent
);

WP