如何查询帖子并使用"orderby"属性根据日期"meta_value"循环订购帖子? [英] How do I query posts and use the 'orderby' attribute to order posts in loop according to date 'meta_value'?

查看:124
本文介绍了如何查询帖子并使用"orderby"属性根据日期"meta_value"循环订购帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一家剧院公司创建一个网站,并且正在为过去,当前和将来的所有作品创建索引.我希望索引可以排序"每个作品的结束日期(ACF日期"字段类型;结束日期").

I am creating a website for a theatre company, and I am creating an index of all past, current, and future productions. I would like the index to 'orderby' the ending date of each production (ACF 'date' field type; 'ending_date').

这是我的查询示例:

 <?php 
  $futureProd = array(
   'post_type' => 'productions',
   'posts_per_page' => -1,
   'meta_key' => 'ending_date',
   'orderby' => 'meta_value',
   'order' => 'ASC',
 );

 $slider_posts = new WP_Query($futureProd);

 $array_rev = array_reverse($slider_posts->posts);
 $slider_posts->posts = $array_rev;

 ?>

还尝试了以下操作,添加了"meta_value_date"和"meta_value_num"替代项:

Have also tried the following, adding the 'meta_value_date' as well as 'meta_value_num' alternatives:

<?php // query posts
  $futureProd = array(
    'post_type' => 'productions',
    'posts_per_page' => -1,
    'meta_key' => 'ending_date',
    'orderby' => 'meta_value_date',            
    'order' => 'ASC',
  );
?>

AND

<?php // query posts
      $futureProd = array(
    'post_type' => 'productions',
    'posts_per_page' => -1,
    'meta_key' => 'ending_date',
    'orderby' => 'meta_value_num',            
    'order' => 'ASC',
  );
?>

无论我尝试什么,这些帖子都拒绝按meta_value对其顺序进行排序,而是选择按默认的发布日期对自己的顺序进行排序.

No matter what I try, the posts refuse to order themselevs by the meta_value, and instead opt to order themselves by the default, post date.

我确定我缺少一些简单的东西.

I'm sure I'm missing something simple.

有人有什么主意吗?

推荐答案

您可以像meta_value_*一样在orderby键中使数据类型具体化 可能的值为"NUMERIC","BINARY","CHAR","DATE","DATETIME", 'DECIMAL','SIGNED','TIME','UNSIGNED'.

You can spicify the datatype in orderby key like meta_value_* possible values are 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.

尝试一下,

$futureProd = array(
    'post_type' => 'productions',
    'posts_per_page' => -1,
    'meta_key' => 'ending_date',
    'orderby' => 'meta_value_date',
    'order' => 'ASC',
);

$slider_posts = new WP_Query($futureProd);

请注意:要使上述代码正常工作,如果日期格式不同,则日期必须为YYYY-MM-DD格式,然后必须创建自定义过滤器函数,并连接到

Please Note: for above code to work you need to have date in YYYY-MM-DD format if you have date in different format then you have to create your custom filter function hook in to posts_orderby filter with STR_TO_DATE MySQL function.


For date format YYYYMMDD

将此添加到活动子主题(或主题)的活动主题function.php文件中.或在任何插件php文件中.

Add this in your active theme function.php file of your active child theme (or theme). Or also in any plugin php files.

function text_domain_posts_orderby($orderby, $query) {
    //Only for custom orderby key
    if ($query->get('orderby') != 'yyyymmdd_date_format')
        return $orderby;
    if (!( $order = $query->get('order') ))
        $order = 'ASC';
    global $wpdb;
    $fieldName = $wpdb->postmeta . '.meta_value';
    return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}

add_filter('posts_orderby', 'text_domain_posts_orderby', 10, 2);

因此,现在您的WP_Query参数应如下所示:

So now your WP_Query argument should look like this:

$futureProd = array(
    'post_type' => 'productions',
    'posts_per_page' => -1,
    'meta_key' => 'ending_date',
    'orderby' => 'yyyymmdd_date_format', //added custom orderby key
    'order' => 'ASC',
);

该代码已经过测试并且功能齐全.

参考:

  • WP_Query Order & Orderby Parameters
  • MySQL ORDER BY Date field which is not in date format

希望这会有所帮助.

这篇关于如何查询帖子并使用"orderby"属性根据日期"meta_value"循环订购帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆