WordPress活动Orderby Meta Value日期 [英] Wordpress Events Orderby Meta Value Date

查看:94
本文介绍了WordPress活动Orderby Meta Value日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这没什么花哨的,应该比这容易得多.也许这更容易,但我只是想得太多?任何帮助将不胜感激.

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.

我在使用插件 http://www.advancedcustomfields.com ,数据库中的字段名称为"event_date".

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".

我已经尝试了各种形式的以下内容:

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;

并且:

$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;

并且:

$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'
        )
    )
));

推荐答案

这最终导致了我需要的解决方案:

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

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

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

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

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');
?>

这篇关于WordPress活动Orderby Meta Value日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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