wp query search
The WP_Query class in WordPress is used to query posts and pages from the database. Here are the basic steps involved in using WP_Query:
- Create a new instance of the WP_Query class by assigning it to a variable:
$query = new WP_Query();
- Define the query parameters using an array. These parameters include post type, category, tags, author, and more. For example:
$args = array(
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 10
);
- Set the query parameters for the WP_Query object by passing the defined arguments array:
$query->query($args);
- Use a loop to iterate through the queried posts and display their content:
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
// Display post content here
endwhile;
wp_reset_postdata(); // Reset post data
endif;