WP_Query Arguments: Categories and Tags


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 WP_Query

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
  • Loop
  • Deletion: Rearrange post data

In practice it will look something like this:

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.

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.

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.

Category parameter

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

cat Parameter is straightforward: use only a Category ID or Category ID string.

Query for a category looks like this:

Querying for multiple categories looks like this:

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.

The above publication will be in category 12 but not in category 13.

category_name parameter

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:

And to find posts in one or more of the multiple categories, use:

As cat Parameters, no posts found in it All Categories, but it will have posts Someone Category

category__and parameter

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:

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.

category__in parameter

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:

The above will bring up one or more of these category posts.

category__not_in parameter

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.

And to exclude posts from an array of categories:

It will not delete any of these types of letters.

Tag parameters

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 parameter

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:

And to find posts tagged with a row of tags:

Note that post with the above questions Someone Of the tags in the row, not all of them.

tag_id parameter

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:

To find posts with one or more tags from a string of tag IDs:

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:

While searching for posts with one of the two tags but without the other tag, you’ll use:

So one or both of the above lines will be with tag 21 or 23 but not tag 22.

tag__in parameter

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:

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 parameter

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:

Note that you still need to use the row even though you are using only one tag. For more tags, use:

It will query posts that do not have the above tags.

tag_slug__and And tag_slug__in Parameters

These two parameters behave exactly the same as tag__and And 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 tag__slug_in:

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 tag_slug__and:

Instead of querying posts with any tags, query only posts that contain All Of tags.

Innovative use with categories and tags WP_Query

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 ‘child_of' Reason.

The code is:

Alternative 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 'posts_per_page' And 'offset' Arguments

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:

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 cat And 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:


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.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top