📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials WordPress Development WP_Query

WP_Query

5 min read Quiz at the end
WP_Query is the primary way to query posts — use meta_query, tax_query, search, and orderby.

WP_Query — Advanced Queries

// Common parameters
$args = [
    'post_type'       => ['post','portfolio'],
    'posts_per_page'  => 12,
    'paged'           => get_query_var('paged'),
    'post_status'     => 'publish',
    'orderby'         => 'meta_value_num',
    'meta_key'        => '_views',
    'order'           => 'DESC',
    'meta_query'      => [[
        'key'     => '_featured',
        'value'   => '1',
        'compare' => '='
    ]],
    'tax_query' => [[
        'taxonomy' => 'skill',
        'field'    => 'slug',
        'terms'    => 'php',
    ]],
    's' => 'search term',
];

// Raw SQL
global $wpdb;
$results = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM {wpdb->posts} WHERE post_author = %d",
        $user_id
    )
);
Topic Quiz · 1 questions

Test your understanding before moving on

1. What should you always call after a custom WP_Query loop?
💡 wp_reset_postdata() restores the global $post variable after a custom WP_Query — forgetting it breaks the main loop.