欢迎光临
我们一直在努力

掌握 WP_Meta_Query 和 WP_Date_Query

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

本文目录
[隐藏]

  • 1Working With All Kinds of Meta Data Through the WP_Meta_Query Class
    • 1.1Example Use of the WP_Meta_Query Class
  • 2Wrangling Date Queries With the WP_Date_Query Class
    • 2.1Example Use of the WP_Date_Query Class
  • 3Wrapping Everything Up

本文是《掌握 WP_Query》系列教程的第 17 部分,该系列共包含以下 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_Meta_Query 和 WP_Date_Query。

废话少说,让我们开始吧!

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

以下为原文:http://code.tutsplus.com/tutorials/mastering-wp_meta_query-wp_date_query–cms-23352

Welcome to the final part of the series—well, technically the final part will be “Series Finale”, but you get the idea. In this part, you’re going to learn about two sibling classes called WP_Meta_Query and WP_Date_Query.

Without further ado, let’s begin!

Working With All Kinds of Meta Data Through the WP_Meta_Query Class

The WP_Meta_Query class is a “helper class”, helping WP_Query to make queries with meta data.

As you know, WordPress stores three kinds of meta data in the database: post meta, user meta and comment meta. We saw in the previous tutorials that we can run meta queries within the queries that we make with the WP_Query, WP_User_Query and WP_Comment_Query classes (with the 'meta_query' parameter). TheWP_Meta_Query is actually running when you do those queries.

Turns out, you can get the SQLs for these meta-related queries with the help of the WP_Meta_Query class. This class doesn’t really get the results of the given query, but instead prepares the SQL commands for you to use somewhere else.

Example Use of the WP_Meta_Query Class

We can’t say it’s a tutorial if we don’t do an example, right? With a simple example, we’re going to see how we can use the WP_Meta_Query class in real life. (Granted, it’s an extremely specific thing to get the SQL code for a meta-related query, but I will try to come up with a real-world example.)

Let’s imagine that you want to make a special “related posts plugin” for your own website, where you will list posts which have the same meta value for a meta key—or another meta value for another meta key. And instead of making a meta query within a WP_Query instance, you want to get the SQL code of the query to use it dynamically in separate code blocks. Here are the steps to prepare that SQL code:

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
<?php
 
global $wpdb;
 
$my_meta_query_args = array(
    'relation' => 'OR',
    array(
        'meta_key' => 'Some_Key',
        'meta_value' => 'Some_Value',
        'compare' => '='
    ),
    array(
        'meta_key' => 'Some_Other_Key',
        'meta_value' => 'Some_Other_Value',
        'compare' => '='
    )
);
 
$my_meta_query = new WP_Meta_Query;
 
$my_meta_query->parse_query_vars( $my_meta_query_args );
 
$my_meta_query_sql = $my_meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
 
?>

<?php global $wpdb; $my_meta_query_args = array( ‘relation’ => ‘OR’, array( ‘meta_key’ => ‘Some_Key’, ‘meta_value’ => ‘Some_Value’, ‘compare’ => ‘=’ ), array( ‘meta_key’ => ‘Some_Other_Key’, ‘meta_value’ => ‘Some_Other_Value’, ‘compare’ => ‘=’ ) ); $my_meta_query = new WP_Meta_Query; $my_meta_query->parse_query_vars( $my_meta_query_args ); $my_meta_query_sql = $my_meta_query->get_sql( ‘post’, $wpdb->posts, ‘ID’ ); ?>

There you go: The $my_meta_sql variable stores the SQL code for your special query, and you can use this SQL code anywhere you like within your project.

Wrangling Date Queries With the WP_Date_Query Class

Just like WP_Meta_Query, the WP_Date_Query is a helper class for the WP_Query, WP_User_Query and WP_Comment_Queryclasses. This helper class was introduced in WordPress version 3.7. Back then, the class didn’t supportWP_User_Query, but since the 4.1 version, you can query inside the users table (the user_registered column specifically).

Similar to WP_Meta_Query and its ability to query meta keys and values, the WP_Date_Query class allows us to query date fields inside the posts, comments and users tables. And exactly like WP_Meta_Query, this helper class also lets you return the prepared SQL code to run a date-related query.

Example Use of the WP_Date_Query Class

In order to fully understand how the WP_Date_Query class works, let’s go through an example with it. It’s going to be yet another unnecessarily specific example, but it wouldn’t feel right to leave this part without an example.

Let’s imagine that, for some reason, we need to query for comments that are made in the current month and before noon. (Please shoot me a comment if you find a good case to fetch comments made in the current month and before noon!) Here’s how to get the SQL code for this bizarre query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
 
$my_date_query_args = array(
    array(
        'month' => date( 'n' ),
    ),
    array(
        'before' => 'noon'
    ),
    'relation' => 'AND'
);
 
$my_date_query = new WP_Date_Query( $my_date_query_args, 'comment_date' );
 
$my_date_query_sql = $my_date_query->get_sql();
 
?>

<?php $my_date_query_args = array( array( ‘month’ => date( ‘n’ ), ), array( ‘before’ => ‘noon’ ), ‘relation’ => ‘AND’ ); $my_date_query = new WP_Date_Query( $my_date_query_args, ‘comment_date’ ); $my_date_query_sql = $my_date_query->get_sql(); ?>

There you go. Keep in mind that you can use PHP relative date formats, which are really useful.

Quick tip: Christian Bruckner has a great post on MarketPress.com about how WP_Date_Query works. It’s a little bit outdated (because it was written before WordPress 4.1 was released) but it’s very well-written and still a good read. Be sure to check it out.

Wrapping Everything Up

With these two helper classes, we’re ending the long journey of dissecting the WP_Query class. This was one of the longest tutorial series in the history of Tuts+, so thank you for bearing with us till the end! In the next (and last) part, we’re going to recap what we went through for the last time and close the series.

阅读该系列的其他文章: 上一篇:掌握 WP_Comment_Query 下一篇:WordPress 4.1的查询改进

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