Tenetur itaque dolorum mollitia facere reprehenderit praesentium ullam. Accusantium id et illo ut eum. Dolore rerum qui sit eveniet ut nesciunt quam. Molestias fugiat omnis laborum quam.
WP_Query Arguments: Categories and Tags

WP_Query Arguments: Categories and Tags

In the earlier parts of this series, you’ve learned how WP_Query is structured and what its properties and methods are. The next stage is to understand the various arguments you can use with it and how best to do so.

WP_Query has a large number of possible arguments, which makes it extremely flexible. As you can use it to query just about anything held in your wp_posts table, it has arguments for every permutation of query you might want to run on your content.

In this tutorial, I’ll look at two types of argument for categories and tags. The arguments for these two taxonomies are similar but do have some differences you need to know about if you’re going to use them effectively.

A Recap on How Arguments Work in WP_Query

Before we start, let’s have a quick recap on how arguments work in WP_Query. When you code WP_Query in your themes or plugins, you need to include four main elements:

  • the arguments for the query, using parameters which will be covered in this tutorial
  • the query itself
  • the loop
  • finishing off: resetting post data

In practice, this will look something like the following:

The arguments are what tells WordPress what data to fetch from the database, and it’s those that I’ll cover here. So all we’re focusing on here is the first part of the code:

As you can see, the arguments are contained in an array. You’ll learn how to code them as you work through this tutorial.

Coding Your Arguments

There is a specific way to code the arguments in the array, which is as follows:

You must enclose the parameters and their values in single quotation marks, use => between them, and separate them with a comma. If you get this wrong, WordPress may not add all of your arguments to the query or you may get a white screen.

Category Parameters

Let’s start with category parameters. The options you have here are as follows:

  • cat (int): use category id.
  • category_name (string): use category slug (NOT name).
  • category__and (array): use category id.
  • category__in (array): use category id.
  • category__not_in (array): use category id.

Note that for none of these do you use the name of your category. Even the category_name parameter takes the slug as its value, not its name. I tend to use this one rather than the ID as when I come back to my code at a later date, slugs are easier to identify than IDs. However, if you think your site users might change the slug for one or more categories, I recommend using the ID to avoid any problems.

Let’s take a look at how you use each of these.

The cat Parameter

The cat parameter is straightforward: just use a single category ID or a string of category IDs.

Querying for one category looks like this:

Querying for multiple categories looks like this:

The above will tell WordPress to fetch posts that are in any of the categories listed. If you want to find posts in every one of an array of categories, you use the category_and parameter, of which more shortly.

You can also use the cat parameter to find posts that are in one category but not another, by using a minus sign before the category ID as follows:

The above would query posts in category 12 but not in category 13.

The category_name Parameter

The category_name parameter uses the category slug, not the name (confusing, I know!). Again, you can use it with a single category or with a string of categories to find posts that are in any of the categories.

To query posts in a single category, you add:

And to find posts in one or more of a number of categories, use this:

As with the cat parameter, this won’t find posts that are in all of the categories, but it will find posts in any of the categories.

The category__and Parameter

If you want to find posts that are in all of an array of categories, this is the parameter you use. It takes the category IDs for its value. So to find posts in all of three categories, you would use:

Note that this uses an array not a string, so you code it differently. The parameter has two underscores in its name: use just one and it won’t work.

The category__in Parameter

The next parameter looks for posts in one or more of an array of categories. It actually works in the same way as the cat parameter, and also takes the category ID as its value.

So to query posts in one or more of an array of categories, you would use:

The above would fetch posts from one or more of these categories.

The category__not_in Parameter

The category__not_in parameter does as you would expect: it queries posts which are not in a category or an array of categories.

To exclude posts from one category, you would use the following:

And to exclude posts from an array of categories:

This would exclude posts from any of these categories.

Tag Parameters

Tags have slightly different parameters from categories: you can’t work out what they might be based on your knowledge of category parameters, I’m afraid!

The tag parameters are:

  • tag (string): use tag slug.
  • tag_id (int): use tag id.
  • tag__and (array): use tag ids.
  • tag__in (array): use tag ids.
  • tag__not_in (array): use tag ids.
  • tag_slug__and (array): use tag slugs.
  • tag_slug__in (array): use tag slugs.

Let’s look at each of these.

The tag Parameter

The tag parameter takes the tag slug for its value and can be used to find posts with one tag or with any of a string of tags.

So to find posts with one tag, you use:

And to find posts with tags from an array of tags:

Note that the above queries posts with any of the tags in the array, not all of them.

The tag_id Parameter

The tag_id parameter works in a similar way to the cat parameter: it takes the tag ID and can be used with a single tag or multiple tags.

To find posts with a single tag, you use this:

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

You can also use tag_id to exclude tags, either when using it for single tags or multiple tags.

So to query posts except those with a given tag, you’d use:

While to find posts with one of two tags but without another tag, you’d use this:

So the above will query posts with either or both of tags 21 or 23 but not tag 22.

The tag__in Parameter

This parameter lets you find posts with one or more of an array of tags. It works in the same way as tag when used with an array:

This will query posts with any or all of the tags listed. If you want to find posts with all of the tags, you use tag__and, which I’ll cover in a moment.

The tag__not_in Parameter

The tag__not_in parameter lets you query posts which don’t have a given tag or array of tags.

Use it like this to exclude one tag:

Note that you still need to use an array even though you’re only using one tag. For more tags, use:

This will query posts that don’t have any of the above tags.

The tag_slug__and and tag_slug__in Parameters

These two parameters behave in exactly the same way as the tag__and and tag__in parameters, except that you use that tag slug in your arrays instead of the tag ID.

So, for example, to find posts which have both of a pair of tags, you use tag__slug_in:

This finds posts with any of these tags. You could also use the tag parameter with a string of tag slugs to achieve the same result.

To include posts with all of a set of tags, use tag_slug__and:

Instead of querying posts with any of the tags, this only queries posts that have all of the tags.

Advanced Use of Categories and Tags With WP_Query

Let’s take a look at some advanced uses of category and tag arguments with WP_Query, based on questions raised in the comments to this article.

List Subcategories of a Category

To list the subcategories of a given category (with a link to the subcategory archives for each), you wouldn’t use WP_Query. Instead, you’d use wp_list_categories(), which outputs a list of categories with links to their archives. To output the subcategories of a specific category, use the ‘child_of' argument.

Here’s the code:

Substitute the 5 in the code above for the ID of the category whose subcategories you want to list.

Display the First Post From a Category in One Element, Then More Posts in Another Element

This is a useful technique if you want to highlight the most recent post from a category, maybe including the excerpt and featured image for that post, and then show a list of the next few posts from the category.

You do this by using the 'posts_per_page' and 'offset' arguments.

The simplest way to do this is to write two separate queries. The first uses 'posts_per_page' => 1 to just fetch the first post. The second query uses 'offset' => 1 to fetch posts after the first post, and also uses 'posts_per_page' to define how many posts to output.

Here’s the code:

An alternative method, which involves fewer calls to the database, is to write one query and then loop back through that query a second time, rewinding the query and using a variable to make sure the post output by the first loop isn’t repeated. 

You can find more about using one query for multiple loops in our tutorial on the subject.

Display Posts From Multiple Categories

If you want to display posts that are in a given category and also in one or more of your other categories, you use the cat and category__in arguments. For each of these, you use the category ID.

Imagine you want your posts to be category 1 and also in one or more of categories 2, 3, and 5.

Here are the arguments you would use for this:


Querying your posts by category and/or tag is something there’s a good chance you’ll have occasion to do with WP_Query. By using the arguments above, and combining them where necessary, you can create powerful arguments to extract exactly the data you need from the database.

If you want to learn more about WP_Query, check out some of my other posts in the Mastering WP_Query series.

Expert Advice: How to Improve Remote Education Collaboration

Expert Advice: How to Improve Remote Education Collaboration

As we’re witnessing with schools and learning communities around the world, education is shifting dramatically. With the right set of tools, your class, team, or group can learn to communicate and collaborate more efficiently online. Since our company was founded over fifteen years ago, the people behind the scenes at WordPress.com have worked from home — or from anywhere they choose in the world — and have learned a lot along the way.

 Image of p2 screen

A tool we call P2 has been indispensable to us, and to a growing number of educators. Want to learn our tips and tricks? Join us for a free webinar on Thursday, November 5, so you and your team can learn to make the most of this tool for remote collaboration. You can also sign up for the free beta version of P2 that is now available.

  • Date: Thursday, November 5, 2020
  • Time: 10:00 am PT | 12:00 pm CT | 1:00 pm ET | 18:00 UTC
  • Registration link: https://zoom.us/webinar/register/4016033198190/WN_WjX8jQhIQ0iZVPpfGAklhQ
  • Who’s invited: Anyone looking to improve internal team collaboration or build a public forum with P2 are welcome, but this webinar is specially designed for educators and teachers.

Register for the webinar today! We look forward to seeing you.

Trump keeps hinting at how desperate his campaign is for votes, telling rally crowds he 'wouldn't be out here' if he didn't need them

Trump keeps hinting at how desperate his campaign is for votes, telling rally crowds he 'wouldn't be out here' if he didn't need them

LANSING, MICHIGAN - OCTOBER 27: Supporters watch a video of U.S. President Donald Trump while waiting in a cold rain for his arrival at a campaign rally at Capital Region International Airport October 27, 2020 in Lansing, Michigan. With one week until Election Day, Trump is campaigning in Michigan, a state he won in 2016 by less than 11,000 votes, the narrowest margin of victory in the state's presidential election history. (Photo by Chip Somodevilla/Getty Images)
A video of President Donald Trump at a campaign rally at Capital Region International Airport in Lansing, Michigan, on October 27, 2020.

  • President Donald Trump told a Michigan rally on Tuesday that he would not be there if it weren’t for the pandemic, hinting yet again that his campaign is in need of more votes.
  • “I probably wouldn’t be standing out here in the freezing rain with you. I’d be home in the White House, doing whatever the hell I was doing,” Trump said in Lansing on Tuesday.
  • Earlier that day, in West Salem, Wisconsin, Trump told a crowd: “What the hell do you think I’m doing here on a freezing night with 45-degree winds? Do you think I’m doing this for my health?”
  • On October 20, Trump had told a crowd in Erie, Pennsylvania, that “before the plague came in, I had it made. I wasn’t coming to Erie. I mean, I have to be honest, there’s no way I was coming.”
  • With five days to Election Day, Biden is maintaining a steady lead over Trump, with polls in Wisconsin — a longtime Democratic state that Trump picked up in 2016 — suggesting Biden has a substantial lead.
  • Visit Business Insider’s homepage for more stories.

President Donald Trump told supporters in Michigan and Wisconsin on Tuesday that he wouldn’t be there if the COVID-19 pandemic had not dealt such a blow to his campaign — and it’s not the first time he’s hinted at how desperate he is for votes.

In the run-up to the November 3 election both Trump and Democratic presidential nominee Joe Biden have traversed the US, holding rallies and racking up media appearances.  

But Trump, on several occasions, has told his supporters that had the pandemic not happened, he would not have needed to visit them.

“It wasn’t even going to be like we had an election,” Trump told a crowd in Lansing on Tuesday. “I probably wouldn’t be standing out here in the freezing rain with you. I’d be home in the White House, doing whatever the hell I was doing. I wouldn’t be out here.”

Trump narrowly won Michigan in 2016, ending six consecutive elections where it was won by the Democratic Party. 

OMAHA, NE - OCTOBER 27: US President Donald Trump speaks during a campaign rally on October 27, 2020 in Omaha, Nebraska. With the presidential election one week away, candidates of both parties are attempting to secure their standings in important swing states. (Photo by Steve Pope/Getty Images)
Trump speaks during a campaign rally in Omaha, Nebraska, on October 27, 2020.

Earlier on Tuesday, Trump also suggested to assembled followers in West Salem, Wisconsin, that he was only there as a last resort.

“What the hell do you think I’m doing here on a freezing night with 45-degree winds?” he said. “Do you think I’m doing this for my health? I’m not doing this for my health.”

And later that day, at a rally in Omaha, Nebraska, Trump said: “I am standing here, freezing. I ask you one little favor: Get the heck out and vote.”

Trump won both Wisconsin — a longtime Democratic state — and Nebraska in 2016. According to recent polling, Biden’s lead in Wisconsin is growing.

Read more: New report: 2020 campaigns are on track to cost $14 billion, obliterating past election races

On October 20, Trump explicitly told a crowd in Erie, a town in the swing state of Pennsylvania, that he had only come because he needed their votes.

“Four or five months ago when we started this whole thing — because, you know, before the plague came in, I had it made. I wasn’t coming to Erie,” Trump said. “I mean, I have to be honest, there’s no way I was coming.”

Washington ballots
A worker processes mailed-in ballots at the King County Elections headquarters in Renton, Washington, on August 5, 2020.

“I didn’t have to,” he continued. “I would’ve called you and said: ‘Hey, Erie. You know, if you have a chance, get out and vote.’ We had this thing won. We were so far up.”

“We had the greatest economy ever, greatest jobs, greatest everything. And then we got hit with the plague, and I had to go back to work. ‘Hello, Erie, may I please have your vote?’ Right?”

And on Tuesday, first lady Melania Trump made her first appearance on the campaign trial in Pennsylvania. As Business Insider’s John L. Dorman reported, the appearance was likely aimed at connecting with those suburban female voters.

As of Wednesday evening, Biden has maintained comfortable national lead over Trump, according to a multi-poll tracker run by FiveThirtyEight.

Read the original article on Business Insider
 Image of silicon alley insider?i=yd55usqoXic:dXr4soifymE:V sGLiPBpWU  Image of silicon alley insider?d=qj6IDK7rITs  Image of silicon alley insider?d=bcOpcFrp8Mo  Image of silicon alley insider?i=yd55usqoXic:dXr4soifymE:gIN9vFwOqvQ  Image of silicon alley insider?d=cGdyc7Q 1BI  Image of silicon alley insider?d=QXVau8BzmBE  Image of silicon alley insider?d=yIl2AUoC8zA  Image of silicon alley insider?d=7Q72WNTAKBA

 Image of yd55usqoXic

Dart-Shooting Drone Attacks Trees for Science

Dart-Shooting Drone Attacks Trees for Science

We all know how robots are great at going to places where you can’t (or shouldn’t) send a human. We also know how robots are great at doing repetitive tasks. These characteristics have the potential to make robots ideal for setting up wireless sensor networks in hazardous environments—that is, they could deploy a whole bunch of self-contained sensor nodes that create a network that can monitor a very large area for a very long time.

When it comes to using drones to set up sensor networks, you’ve generally got two options: A drone that just drops sensors on the ground (easy but inaccurate and limited locations), or using a drone with some sort of manipulator on it to stick sensors in specific places (complicated and risky). A third option, under development by researchers at Imperial College London’s Aerial Robotics Lab, provides the accuracy of direct contact with the safety and ease of use of passive dropping by instead using the drone as a launching platform for laser-aimed, sensor-equipped darts.