In WordPress, the WP_Query class is used to retrieve and display posts from the website’s database. One of the parameters that can be passed to the WP_Query class is the “cat” parameter, which is used to display posts from a specific category. The “cat” parameter can be used in several ways to display posts from one or multiple categories, as well as exclude posts from certain categories.

  • 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.

Show Posts for One Category:

The “cat” parameter is used to specify the category ID of the category from which posts should be retrieved. For example, the following code retrieves and displays all posts from category ID 4:

$query = new WP_Query( 'cat=4' );

In addition to the “cat” parameter, WordPress also offers the “category_name” parameter, which is used to specify the category slug instead of the category ID. The category slug is the URL-friendly version of the category name, and can be found in the WordPress admin area under Posts > Categories. For example, the following code retrieves and displays all posts from the category with the slug “staff”:

$query = new WP_Query( 'category_name=staff' );

Display posts that have this category (not children of that category), using category id:

$query = new WP_Query( 'category__in=4' );

Show Posts from Several Categories

If you want to retrieve and display posts from multiple categories, you can use the “cat” parameter with a comma-separated list of category IDs. For example, the following code retrieves and displays all posts from categories 2, 6, 17, and 38:

$query = new WP_Query( 'cat=2,6,17,38' );

You can also use the “category_name” parameter with a comma-separated list of category slugs. For example, the following code retrieves and displays all posts from categories with slugs “staff” and “news”:

$query = new WP_Query( 'category_name=staff,news' );

Exclude Posts Belonging to Category

To exclude posts from certain categories, you can prefix the category ID with a “-” (minus) sign. For example, the following code retrieves and displays all posts except those from categories 12, 34, and 56:

$query = new WP_Query( 'cat=-12,-34,-56' );

Multiple Category Handling

WordPress also offers the “category__in” and “category__not_in” parameters, which allow you to specify an array of category IDs to include or exclude, respectively. The “category__and” parameter allows you to retrieve and display posts that are in multiple categories.

For example, the following code retrieves and displays all posts that are in both categories 2 and 6:

$query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

To display posts from either category 2 OR 6, you could use cat as mentioned above, or by using category__in (note this does not show posts from any children of these categories):

$query = new WP_Query( array( 'category__in' => array( 2, 6 ) ) );

You can also exclude multiple categories this way:

$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

Conclusion:

In conclusion, the WP_Query class in WordPress offers several parameters for retrieving and displaying posts from specific categories, including the “cat” parameter, which is used to specify the category ID, and the “category_name” parameter, which is used to specify the category slug. Additionally, the “category__in”, “category__not_in”, and “category__and” parameters allow you to specify an array of category IDs to include, exclude, or retrieve posts that are in multiple categories respectively.