欢迎光临
我们一直在努力

WP_Query 参数:分类和标签

建站超值云服务器,限时71元/月

本文目录
[隐藏]

  • 1A Recap on How Arguments Work in WP_Query
    • 1.1Coding Your Arguments
  • 2Category Parameters
    • 2.1The cat Parameter
    • 2.2The category_name Parameter
    • 2.3The category__and Parameter
    • 2.4The category__in Parameter
    • 2.5The category__not_in Parameter
  • 3Tag Parameters
    • 3.1The tag Parameter
    • 3.2The tag_id Parameter
    • 3.3The tag__in Parameter
    • 3.4The tag__not_in Parameter
    • 3.5The tag_slug__and and tag_slug__in Parameters
  • 4Summary

本文是《掌握 WP_Query》系列教程的第 7 部分,该系列共包含以下 19 个部分:

  1. 掌握 WP_Query : 入门介绍
  2. 掌握 WP_Query:教你使用Loop循环
  3. 掌握 WP_Query:相关的函数
  4. 掌握 WP_Query:行动器和过滤器
  5. 掌握 WP_Query:WP_Query类的属性和方法
  6. WP_Query 参数:文章、页面和文章类型
  7. WP_Query 参数:分类和标签
  8. WP_Query 参数:分类法(Taxonomies)
  9. WP_Query 参数:自定义字段(Custom Fields)
  10. WP_Query 参数:日期
  11. WP_Query 参数:状态、排序和分页
  12. WP_Query 参数:作者、搜索、密码、权限、缓存和返回字段
  13. 掌握 WP_Query:10个有用的例子
  14. 结合 WP_Query 与主查询(the Main Query)
  15. 掌握 WP_User_Query
  16. 掌握 WP_Comment_Query
  17. 掌握 WP_Meta_Query 和 WP_Date_Query
  18. WordPress 4.1的查询改进
  19. 掌握 WP_Query:结尾

在这个系类的前面的部分,你已经学习了 WP_Query 的结构、属性和方法。下一个阶段将要掌握如何使用 WP_Query 的各种参数。

WP_Query 有很多可用的参数,使它变得非常灵活。正如你可以使用它来查询存储在  wp_posts 表的任何内容一样,它有参数可用于不同的查询阵列获取你想要的内容。

注:由于时间精力有限,本教程没办法翻译分享,希望朋友们可以加入我们,帮助我们进行翻译,有小酬谢,有意者请联系倡萌QQ 745722006(注明:教程翻译)。

以下为原文:http://code.tutsplus.com/tutorials/wp_query-arguments-categories-and-tags–cms-23070

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 the following:

  • categories
  • 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
 
$args = array(
    // Arguments for your query.
);
 
// 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();
 
?>

<?php $args = array( // Arguments for your query. ); // 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(); ?>

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:

1
2
3
$args = array(
    // Arguments for your query.
);

$args = array( // Arguments for your query. );

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:

1
2
3
4
5
$args = array(
    'parameter1' => 'value',
    'parameter2' => 'value',
    'parameter3' => 'value'
);

$args = array( ‘parameter1’ => ‘value’, ‘parameter2’ => ‘value’, ‘parameter3’ => ‘value’ );

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:

1
2
3
$args = array(
    'cat' => '12'
);

$args = array( ‘cat’ => ’12’ );

Querying for multiple categories looks like this:

1
2
3
$args = array(
    'cat' => '12, 13, 14'
);

$args = array( ‘cat’ => ’12, 13, 14′ );

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:

1
2
3
$args = array(
    'cat' => '12, -13'
);

$args = array( ‘cat’ => ’12, -13′ );

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:

1
2
3
$args = array(
    'category_name' => 'my-slug'
);

$args = array( ‘category_name’ => ‘my-slug’ );

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

1
2
3
$args = array(
    'category_name' => 'my-slug, your-slug, another-slug'
);

$args = array( ‘category_name’ => ‘my-slug, your-slug, another-slug’ );

As with the cat parameter, this won’t find posts that are in all of the categories, but it will find posts in anyof 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:

1
2
3
4
5
6
7
$args = array(
    'category__and' => array(
        '12',
        '13',
        '14'
    )
);

$args = array( ‘category__and’ => array( ’12’, ’13’, ’14’ ) );

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:

1
2
3
4
5
6
7
$args = array(
    'category__in' => array(
        '12',
        '13',
        '14'
    )
);

$args = array( ‘category__in’ => array( ’12’, ’13’, ’14’ ) );

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:

1
2
3
$args = array(
    'category__not_in' => '12'
);

$args = array( ‘category__not_in’ => ’12’ );

And to exclude posts from an array of categories:

1
2
3
4
5
6
7
$args = array(
    'category__not_in' => array(
        '12',
        '13',
        '14'
    )
);

$args = array( ‘category__not_in’ => array( ’12’, ’13’, ’14’ ) );

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:

1
2
3
$args = array(
    'tag' => 'my-tag'
);

$args = array( ‘tag’ => ‘my-tag’ );

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

1
2
3
$args = array(
    'tag' => 'my-tag, your-tag, another-tag'
);

$args = array( ‘tag’ => ‘my-tag, your-tag, another-tag’ );

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:

1
2
3
$args = array(
    'tag_id' => '21'
);

$args = array( ‘tag_id’ => ’21’ );

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

1
2
3
$args = array(
    'tag_id' => '21, 22, 23'
);

$args = array( ‘tag_id’ => ’21, 22, 23′ );

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:

1
2
3
$args = array(
    'tag_id' => '-21'
);

$args = array( ‘tag_id’ => ‘-21’ );

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

1
2
3
$args = array(
    'tag_id' => '21, -22, 23'
);

$args = array( ‘tag_id’ => ’21, -22, 23′ );

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 tagwhen used with an array:

1
2
3
4
5
6
7
$args = array(
    'tag_in' =>  array(
        '21',
        '22',
        '23'
    )
);

$args = array( ‘tag_in’ => array( ’21’, ’22’, ’23’ ) );

This will query posts with any or all of the tags listed. If you want to find posts with all of the tags, you usetag__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:

1
2
3
$args = array(
    'tag__not_in' => array( '21' )
);

$args = array( ‘tag__not_in’ => array( ’21’ ) );

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

1
2
3
4
5
6
7
$args = array(
    'tag__not_in' => array(
        '21',
        '22',
        '23'
    )
);

$args = array( ‘tag__not_in’ => array( ’21’, ’22’, ’23’ ) );

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:

1
2
3
4
5
6
7
$args = array(
    'tag_slug__in' => array(
        'my-tag',
        'your-tag',
        'another-tag'
    )
);

$args = array( ‘tag_slug__in’ => array( ‘my-tag’, ‘your-tag’, ‘another-tag’ ) );

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:

1
2
3
4
5
6
7
$args = array(
    'tag_slug__and' => array(
        'my-tag',
        'your-tag',
        'another-tag'
    )
);

$args = array( ‘tag_slug__and’ => array( ‘my-tag’, ‘your-tag’, ‘another-tag’ ) );

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

Summary

阅读该系列的其他文章: 上一篇:WP_Query 参数:文章、页面和文章类型 下一篇:WP_Query 参数:分类法(Taxonomies)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WP_Query 参数:分类和标签
分享到: 更多 (0)