php Wordpress 事件 Orderby Meta 生效日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24045179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:07:56  来源:igfitidea点击:

Wordpress Events Orderby Meta Value Date

phpwordpressplugins

提问by Jordan

I'm having some trouble getting an arguments array to sort an event list by the date in Wordpress. I've found several suggested solutions here on Stack Overflow and elsewhere, but none of the solutions seem to work after lots of trial and error.

我在获取参数数组以按 Wordpress 中的日期对事件列表进行排序时遇到了一些麻烦。我在 Stack Overflow 和其他地方找到了几个建议的解决方案,但经过大量反复试验,这些解决方案似乎都不起作用。

It's nothing fancy, and it should be a lot easier than this. Maybe it is easier and I'm just overthinking it? Any help is greatly appreciated.

没什么特别的,应该比这容易很多。也许它更容易,我只是想多了?任何帮助是极大的赞赏。

I've got a custom Date Field inside the Post using the plugin http://www.advancedcustomfields.com, field name in the database is "event_date".

我使用插件http://www.advancedcustomfields.com在 Post 中有一个自定义日期字段,数据库中的字段名称是“event_date”。

I've tried the following in various forms:

我尝试了以下各种形式:

$args = array(
     'post_status'       => 'publish',
     'meta_key'          => 'event_date',
     'orderby'           => 'meta_value_num',
     'order'             => 'DESC',
     'posts_per_page'    => 6,
     'paged'             => $paged,
     'post__not_in'      => $exclude_array
);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

$default_excerpt_length = 250;

And:

和:

$args = array(
    'post_status'       => 'publish',
    'meta_key'          => 'event_date',
    'meta_value_num'    => time(),
    'meta_compare'      => '>=',
    'orderby'           => 'meta_value_num',
    'order'             => 'DESC',
    'posts_per_page'    => 6,
    'paged'             => $paged,
    'post__not_in'      => $exclude_array
);

$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);

$default_excerpt_length = 250;

And:

和:

$today = date('Y-m-d');

query_posts(array(
    'post_type'         => 'events',
    'posts_per_page'    => 6,
    'paged'             => $paged,
    'meta_key'          => 'event_date',
    'orderby'           => 'meta_value',
    'order'             => 'DESC',
    'meta_query' => array(
        array(
        'key'        => 'event_date',
        'meta-value' => $value,
        'value'      => $today,
        'compare'    => '>=',
        'type'       => 'CHAR'
        )
    )
));

回答by Jordan

This is eventually what led to the solution I needed: http://www.advancedcustomfields.com/resources/field-types/date-picker/

这最终导致了我需要的解决方案:http: //www.advancedcustomfields.com/resources/field-types/date-picker/

I ended up changing the Advanced Custom Field setting to yymmdd as recommend via that url.

我最终通过该网址将高级自定义字段设置更改为 yymmdd。

This is what I used to query the posts:

这是我用来查询帖子的内容:

$args = array(
  'post_status'       => 'publish',
  'posts_per_page'    => 6,
  'paged'             => $paged,
  'meta_key'          => 'event_date',
  'orderby'           => 'meta_value_num',
  'order'             => 'ASC'
);

And this is what I used to adjust the visual output of the date on the page:

这是我用来调整页面上日期的视觉输出的:

<?php
$source = get_field('event_date');
$date = new DateTime($source);
echo $date->format('F j, Y');
?>

回答by WongKongPhooey

After searching multiple similar posts, I put a solution together from parts of various other SO posts, hopefully it can help somebody else.

在搜索了多个类似的帖子后,我将各种其他 SO 帖子的部分内容放在一起,希望它可以帮助其他人。

I had a custom meta key called "date" (poor naming convention, I know), and this query shows all posts with a custom "date" meta field in the future, sorting by closest to today.

我有一个名为“date”的自定义元键(我知道,命名约定很差),这个查询显示未来所有带有自定义“date”元字段的帖子,按最接近今天的顺序排序。

$args = array(
        'post_type' => 'training-course',
        'posts_per_page' => '-1',
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(
            array(
                'key' => 'date',
                'value' => date("Ymd"), // date format error
                'compare' => '<='
            )                   
         )
    );