Working with WP Query
What is WP Query
WP_Query is a class, one of the most important in WordPress core. It is in charge of determining the necessary query to the database towards the post types, according to the information that is being requested and, in addition, it saves this type of frequent queries to optimize the loading of the page.
When to use WP Query?
The main form of use is when we want to show the articles of our blog or any other post type.
For example:
- When we are going to list the products of our online store.
- You can use it to load articles related to a post.
- For your important news slider.
As you can see, the WordPress structure is focused on posts, therefore, you will use the WP Query everywhere. All!
Let’s do it
Think that you want to consult the last 10 posts that you created in your blog, for this we are going to instantiate the WP_Query class, to which we will send the parameter “post_per_page” which is the one that limits the number of entries that we want to return, and later we will create a loop that runs through all posts consulting the title of the same and finally a separator of type hr
<?php
$query = new WP_Query(array(
"posts_per_page" => 10
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>Now, we will limit the results that the function returns to only 3 results, for this we include or modify the “posts_per_page” option and from now on we will call a “Custom post type” that we will call “my_custom_post_type”
<?php
$query = new WP_Query(array(
"post_type" => "my_custom_post_type",
"posts_per_page" => 10
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>The WP_Query class also allows us to restrict the query, including the ‘meta_query’ option that searches the ‘post meta’ of the queried post types. For this example we will query our “my_custom_post_type” containing the “post meta” called “featured” set to true.
<?php
$query = new WP_Query(array(
"post_type" => "my_custom_post_type",
"posts_per_page" => 10,
"meta_query" => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '=',
),
),
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>Let’s imagine that now we need the featured posts but that they correspond to a specific category:
<?php
$query = new WP_Query(array(
"post_type" => "my_custom_post_type",
"posts_per_page" => 10,
"meta_query" => array(
array(
'key' => 'featured',
'value' => true,
'compare' => '=',
),
),
"tax_query" => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'technology',
),
),
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>Note: You can call the category by its slug, the name or the id of the category
Now, the WP_Query class allows us to separate the author from the post, let’s imagine that we want to make a page that seeks to create a profile of a blog writer, for this scenario the “author” option is used to specify the author’s ID , or “author_name” for the nice-name of the same
<?php
$query = new WP_Query(array(
"post_type" => "my_custom_post_type",
"posts_per_page" => 10,
"author" => 123,
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>Using “author_name”
<?php
$query = new WP_Query(array(
"post_type" => "my_custom_post_type",
"posts_per_page" => 10,
"author_name" => "sebasvergarap", //User nice-name
));
if ($query->have_posts()) {
while ($query->have_posts()) { $query->the_post();
the_title();
echo "<hr>";
}
}
?>Final thoughts
As evidenced in this post, the WP_Query class allows us to expand the possibilities with WordPress and you can almost filter as much as possible by implementing the correct option within the arguments. To see the full list of possibilities, visit the WP_Query entry on the official WordPress site