In the earlier parts of this series, you learned how
WP_Query Is configured and what are its features and methods. The next step is to understand the different arguments you can use with it and the best way to do it.
WP_Query There are a number of possible arguments, which makes it extremely flexible. As you can use it to inquire about anything inside you
wp_posts Table, contains all the query permission arguments you want to run on your content.
In this tutorial, I will look at two types of arguments for categories and tags:
The two classification arguments are the same but you have some differences that you need to know if you are going to use them effectively.
A confirmation of how arguments work
Before we get started, let’s take a quick look at how arguments work
WP_Query. When you code
WP_Query In your themes or plugins, you need to add four key elements.
- Query arguments, using parameters that will be included in this tutorial
- The query itself
- Deletion: Rearrange post data
In practice it will look something like this:
have_posts() ) // Start looping over the query results. while ( $query->have_posts() ) $query->the_post(); // Contents of the queried post results go here. // Restore original post data. wp_reset_postdata(); ?>
Arguments are what tell WordPress what data to fetch from the database and that’s what I’m covering here. So what we’re focusing on here is the first part of the code.
$args = array( // Arguments for your query. );
As you can see, the arguments are in a row. As you work through this tutorial, you will learn how to code them.
Coding your arguments
There is a special way to rule out arguments in a row, which is as follows.
$args = array( 'parameter1' => 'value', 'parameter2' => 'value', 'parameter3' => 'value' );
You must associate parameters and their values in the same quotation marks, use => between them, and separate them with commas. If you find it incorrect, WordPress may not include all your arguments in the query or you may get a white screen.
Let’s start with the category parameters. Here are the options you have:
cat(INT): Use Category ID.
category_name(the wire): Use category slug (not name).
category__and(سرنی): Use Category ID.
category__in(سرنی): Use Category ID.
category__not_in(سرنی): Use Category ID.
Note that you do not use your category name for any of these. even
category_name The parameter takes this slug as its value, not the name. I use it instead of ID because when I come back to my code at a later date it’s easier to identify instead of IDs. However, if you think that users of your site may change slugs in one or more categories, I suggest using ID to avoid any hassle.
Let’s take a look at how you use each of them.
cat Parameter is straightforward: use only a Category ID or Category ID string.
Query for a category looks like this:
$args = array( 'cat' => '12' );
Querying for multiple categories looks like this:
$args = array( 'cat' => '12, 13, 14' );
The above will tell WordPress to retrieve posts that fall into the categories listed. If you want to find posts in a single row of categories, use them
category_and Parameters, of which shortly.
You can also use
cat Use the minus sign before the category ID to search for posts that are in one category but not another.
$args = array( 'cat' => '12, -13' );
The above publication will be in category 12 but not in category 13.
category_name Uses parameter category slog, not name (confuse, I know!). Again, you can use it with a single category or with a category string to find posts in any category.
To query posts in one of the categories you add:
$args = array( 'category_name' => 'my-slug' );
And to find posts in one or more of the multiple categories, use:
$args = array( 'category_name' => 'my-slug, your-slug, another-slug' );
cat Parameters, no posts found in it All Categories, but it will have posts Someone Category
If you want to find posts that are in all categories, this is the parameter you use. Takes category IDs for its value. So to find posts in these three categories, you will use:
$args = array( 'category__and' => array( '12', '13', '14' ) );
Note that it uses a row, not a string, so you code it differently. There are two underscores in the parameter name: use only one and it won’t work.
The next parameter searches for letters in one or more categories. It actually works the same way
cat Also takes the category ID as the parameter and its value.
Therefore, to query posts in one or more categories, you will use:
$args = array( 'category__in' => array( '12', '13', '14' ) );
The above will bring up one or more of these category posts.
category__not_in The parameter does what you expect: it queries posts that are not in a category or are not included in the category.
To exclude posts from a category, you will use the following.
$args = array( 'category__not_in' => '12' );
And to exclude posts from an array of categories:
$args = array( 'category__not_in' => array( '12', '13', '14' ) );
It will not delete any of these types of letters.
Tags have slightly different parameters than categories: you can’t work based on your knowledge of category parameters, I’m afraid!
The tag parameters are:
tag(the wire): Use the tag slug.
tag_id(INT): Use Tag ID.
tag__and(سرنی): Use tag ads.
tag__in(سرنی): Use tag ads.
tag__not_in(سرنی): Use tag ads.
tag_slug__and(سرنی): Use tag slugs.
tag_slug__in(سرنی): Use tag slugs.
Let’s take a look at each of them.
tag The parameter tag takes the slug for its value and can be used to find posts with a tag or a string of tags.
So finding posts with the tag you use:
$args = array( 'tag' => 'my-tag' );
And to find posts tagged with a row of tags:
$args = array( 'tag' => 'my-tag, your-tag, another-tag' );
Note that post with the above questions Someone Of the tags in the row, not all of them.
tag_id The parameter works the same way for cat parameters: it takes a tag ID and can be used with the same tag or with more than one tag.
To find posts with the same tag you use:
$args = array( 'tag_id' => '21' );
To find posts with one or more tags from a string of tag IDs:
$args = array( 'tag_id' => '21, 22, 23' );
You can also use
tag_id To exclude tags when used for single tags or multiple tags.
So in addition to inquiring about posts, you will use posts with these tags:
$args = array( 'tag_id' => '-21' );
While searching for posts with one of the two tags but without the other tag, you’ll use:
$args = array( 'tag_id' => '21, -22, 23' );
So one or both of the above lines will be with tag 21 or 23 but not tag 22.
This parameter allows you to search for posts with one or more of the same row of tags. It works similarly to tags when used with an array:
$args = array( 'tag_in' => array( '21', '22', '23' ) );
It will query posts with any or all tags. If you want to find posts with all tags you will use
tag__and, Which I will cover in a moment.
tag__not_in The parameter allows you to query posts that have no tags or array of tags.
Here’s how to delete a tag:
$args = array( 'tag__not_in' => array( '21' ) );
Note that you still need to use the row even though you are using only one tag. For more tags, use:
$args = array( 'tag__not_in' => array( '21', '22', '23' ) );
It will query posts that do not have the above tags.
These two parameters behave exactly the same as
tag__in Parameters, except that you use this tag slug in your ranks instead of the tag ID.
So, for example, find posts that have both pairs of tags
$args = array( 'tag_slug__in' => array( 'my-tag', 'your-tag', 'another-tag' ) );
It contains posts with any of these tags. You can also use the tag parameter with a string of tag slugs to get the same result.
To add posts with all tags set, use
$args = array( 'tag_slug__and' => array( 'my-tag', 'your-tag', 'another-tag' ) );
Instead of querying posts with any tags, query only posts that contain All Of tags.
Innovative use with categories and tags
Let’s take a look at some of the latest uses of the category and tag arguments
WP_Query, Based on questions raised in the comments on this article.
List of subcategories of a category
For a list of subcategories of a category (with a link to the subcategory archive for each), you will not use
WP_Query. You will use it instead
wp_list_categories(), Which presents a list of categories with links to its archives. To output a subcategory of a specific category
The code is:
5 ); wp_list_categories( $args ); ?>
5 In the code above for the type ID of the type of subcategories you want to list.
Show the first post from a category in one element then more posts in the second element
This is a useful technique if you want to highlight the latest post in a category, add a quote and highlight icon for that post, and then show a list of a few posts from the category.
You do this using
The easiest way to do this is to write two separate questions. Uses first
'posts_per_page' => 1 Only bring the first post. Uses the second query
'offset' => 1 Retrieves and uses posts after the first post
'posts_per_page' To specify how many posts to output.
The code is:
2, 'posts_per_page' => 1 ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) // Start looping over the query results. while ( $query->have_posts() ) $query->the_post(); // Contents of the queried post results go here. // Restore original post data. wp_reset_postdata(); // second query - three posts $args = array( 'cat' => 2, 'posts_per_page' => 3, 'offset' => 1 ); // Custom query. $query = new WP_Query( $args ); // Check that we have query results. if ( $query->have_posts() ) // Start looping over the query results. while ( $query->have_posts() ) $query->the_post(); // Contents of the queried post results go here. // Rewind the query. rewind_posts(); ?>
An alternative method, which involves fewer calls to the database, is to write a query and then return a second loop through the query, rewind the query, and use variables to ensure that the first loop Post output is not repeated.
You can learn more about using one question for multiple loops in our tutorial on this topic.
Show posts from multiple categories
If you want to post posts that are in a given category and one or more of your other categories, you will use them
category__in Arguments You use the category ID for each of these.
Imagine that you want your posts to be in category 1 and also in one, multiple categories 2, 3 and 5.
You will use the following arguments for this:
$args = array( 'cat' => '1', 'category__in' => array ( '2', '3', '5' ) );
Querying your posts by category and / or tag is a great opportunity that will give you a chance.
WP_Query. Using the above arguments, and combining them where necessary, you can create powerful arguments to extract the required data from the database.
If you’d like to learn more about WP_Query, check out some of my other posts in the Mastering WP_Query Series.
WordPressMastering WP_Query: Using a Loop
WordPressMastering WP_Query: Processes and Filters
WordPressWP_Query Arguments: Status, Order, and Pagination
WordPressMastering WP_Query: 10 Useful Examples