欢迎光临
我们一直在努力

WordPress 4.1的查询改进

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

本文目录
[隐藏]

  • 1Nested Queries
    • 1.1
      • 1.1.1
        • 1.1.1.1Applying Nested Queries: Taxonomy Terms, Metadata, and Dates
  • 2Comment Parameters
  • 3Bug Fixes
  • 4Summary

本文是《掌握 WP_Query》系列教程的第 18 部分,该系列共包含以下 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:结尾

在过去的一年里,很多人都写到 WordPress 改进了用户界面:最受关注的变化是提高写作经验。但如果你是开发者,你可能更想知道 WordPress 底层有哪些改进。在这里,我将演示对开发者最有趣的变化之一:改进某些类型的查询。

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

以下为原文:http://code.tutsplus.com/tutorials/query-improvements-in-the-latest-versions-of-wordpress–cms-23016

Within the last year, much has been written about the improvements to the WordPress user interface: the most talked about change has been the improved writing experience.

But if you’re a developer you’ll be wanting to know less about that and more about what’s changed under the hood. Here I’ll demonstrate one of the most interesting changes for developers: improvements to certain types of queries.

The main changes are as follows:

  • Support for nested queries has been added for queries on post metadata, dates, and taxonomy terms.
  • Extra parameters have been added for querying comments.
  • And some bugs have been fixed too!

Let’s have a look at the changes.

Nested Queries

In previous versions of WordPress, you could use an AND or OR statement to define queries for taxonomy terms, dates, and metadata. So for example, the following arguments will be used in a query on a recipe site which outputs quick breakfast recipes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
 
$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'speed',
            'field' => 'slug',
            'terms' => array( 'quick' )
        ),
        array(
            'taxonomy' => 'meal',
            'field' => 'slug',
            'terms' => array( 'breakfast' )
        )
    )
);
 
$query = new WP_Query( $args );
 
?>

<?php $args = array( ‘post_type’ => ‘post’, ‘tax_query’ => array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘speed’, ‘field’ => ‘slug’, ‘terms’ => array( ‘quick’ ) ), array( ‘taxonomy’ => ‘meal’, ‘field’ => ‘slug’, ‘terms’ => array( ‘breakfast’ ) ) ) ); $query = new WP_Query( $args ); ?>

This looks for the 'speed' and 'meal' taxonomies and outputs posts with the 'quick' and 'breakfast' terms respectively.

But what if you wanted to write a more complex query? Let’s say you wanted quick recipes for breakfast and slow recipes for lunch (maybe for someone who wants to get breakfast done quickly so they have more time to cook lunch!). You don’t want to use a simple AND statement to join all the elements of your query, since then you would get slow recipes for breakfast and lunch, for example. And you don’t want to use an OR statement linking all the terms, as you’ll get all manner of recipes which only have one of the queried terms along with other ones you want to filter out.

The good news is that now you can do this. To query quick recipes for breakfast and slow recipes for lunch, you’d use 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
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
 
$query = new WP_Query(
    array(
        'tax_query' => array(
            'relation' => 'OR',
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'meal',
                    'field' => 'slug',
                    'terms' => array( 'breakfast' )
                ),
                array(
                    'taxonomy' => 'speed',
                    'field' => 'slug',
                    'terms' => array( 'quick' )
                )
            ),
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'meal',
                    'field' => 'slug',
                    'terms' => array( 'lunch' )
                ),
                array(
                    'taxonomy' => 'speed',
                    'field' => 'slug',
                    'terms' => array( 'slow' )
 
                )
            )
        )
    )
);
 
$query = new WP_Query( $args );
 
?>

<?php $query = new WP_Query( array( ‘tax_query’ => array( ‘relation’ => ‘OR’, array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘meal’, ‘field’ => ‘slug’, ‘terms’ => array( ‘breakfast’ ) ), array( ‘taxonomy’ => ‘speed’, ‘field’ => ‘slug’, ‘terms’ => array( ‘quick’ ) ) ), array( ‘relation’ => ‘AND’, array( ‘taxonomy’ => ‘meal’, ‘field’ => ‘slug’, ‘terms’ => array( ‘lunch’ ) ), array( ‘taxonomy’ => ‘speed’, ‘field’ => ‘slug’, ‘terms’ => array( ‘slow’ ) ) ) ) ) ); $query = new WP_Query( $args ); ?>

Here I’ve used two nested arrays:

  • The outer array uses OR, because we’re looking for posts which are either quick breakfast recipes or slow lunch recipes.
  • The first nested array looks for posts which are quick breakfast recipes, using AND because you want the post to have both terms.
  • The second nested array looks for slow lunch recipes, again using AND.

Of course you could vary your queries to include multiple taxonomy terms and values, and get as complex as you need.

Applying Nested Queries: Taxonomy Terms, Metadata, and Dates

The example I’ve given above uses taxonomy terms, but this feature has also been added to date and metadata queries. Metadata is potentially where things could get interesting, as you have the scope for so many values.

The syntax works in exactly the same way for date and metadata queries. For meta queries you replacetax_query with meta_query and use 'key' and 'value' as the parameters. For date queries you replacetax_query with date_query and use the date parameters provided in the WordPress Codex.

Comment Parameters

To query comments, you use the WP_Comment_Query class in place of the more commonly used WP_Query class. This class has had eight new parameters added to it:

  • 'author__in': identify comment author (or an array of authors)
  • 'author__not_in': identify comments not by a certain author (or array of authors)
  • 'post_author__in': identify author (or array of authors) of the post the comment was made on
  • 'post_author__not_in': exclude comments made on posts written by particular author or array of authors
  • 'comment__in': comments with a certain ID or array of IDs
  • 'comment__not_in': exclude comments with a certain ID or array of IDs
  • 'post__in': comments made on a post or array of posts (using the post ID)
  • 'post__not_in': exclude comments made on a post or array of posts (using the post ID)

The values used for these are the author ID, comment ID or post ID as appropriate.

Note that the WP_Comment_Query class now supports nested queries as well.

Bug Fixes

There have also been a couple of bug fixes which you might find helpful:

  • A bug that caused queries to fail when a date_query was used along with a tax_query or meta_query has been fixed.
  • When ‘orderby' => 'meta_value’ was used when passing a 'meta_query' with the OR relation  in WP_Query, this used to break the query. This has been fixed.

If you want the lowdown on all the details, you can find it on the make WordPress core site.

Summary

阅读该系列的其他文章: 上一篇:掌握 WP_Meta_Query 和 WP_Date_Query 下一篇:掌握 WP_Query:结尾

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