简体   繁体   English

在“开始”和“结束”日期自定义字段之间显示当前的Wordpress帖子

[英]Show current Wordpress posts between “start” and “end” date custom fields

So I am trying to show only current events that fall between the start date (st_date) and the end date (end_date) custom fields. 因此,我试图仅显示介于自定义字段开始日期(st_date)和结束日期(end_date)之间的当前事件。 Currently this is not working at all for the current events. 当前,这对于当前事件根本不起作用。

My current time on the Wordpress install is correct as per my timezone (eastern) at -5. 根据我的时区(东部),我当前在Wordpress安装上的时间是正确的-5。

I have tried to change the date format on my Wordpress site to match Ymd, but since the upcoming and previous events are showing up, it obviously didn't help me out. 我试图更改Wordpress网站上的日期格式以匹配Ymd,但是由于即将发生的事件和以前的事件都在显示,所以它显然没有帮助我。

Is it the server time that it's working off of? 是要用的服务器时间吗? As of right now on 12/31, it is showing an event with a start date of 12/30, but not the event I created with a start date of 12/31. 截至目前的12/31,它显示的事件的开始日期为12/30,但未显示我创建的事件的开始日期为12/31。

Here is my current code. 这是我当前的代码。 Any ideas on what needs to changed? 关于需要更改的任何想法? Thanks for any help! 谢谢你的帮助!

    //FILER FOR SEPARATING UPCOMING, CURRENT AND PAST EVENTS
function event_where($where)
{
    global $wpdb,$wp_query;
    $current_term = $wp_query->get_queried_object();
    if((is_archive() || is_tag()) && ($current_term->taxonomy==CUSTOM_CATEGORY_TYPE1 || $current_term->taxonomy==CUSTOM_TAG_TYPE1))
    {
        if($current_term->taxonomy == CUSTOM_CATEGORY_TYPE1 || $current_term->taxonomy == CUSTOM_TAG_TYPE1)
        {
            if(@$_REQUEST['etype']=='')
            {
                $_REQUEST['etype']='current';
            }
            if(@$_REQUEST['etype']=='current')
            {
                $today = date('Y-m-d G:i:s');
                $where .= "  AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='st_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <='".$today."')) AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') > '".$today."')) ";
            }
            elseif($_REQUEST['etype']=='upcoming')
            {
                $today = date('Y-m-d G:i:s');
                $where .= " AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='st_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') >'".$today."' and $wpdb->posts.post_status = 'publish')) ";
            }
            elseif($_REQUEST['etype']=='past')
            {
                $today = date('Y-m-d G:i:s');
                $where .= " AND ($wpdb->posts.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') < '".$today."')) ";
            }
        }elseif(is_day() || is_month() || is_year())
        {
            $where = str_replace("'post'","'".CUSTOM_POST_TYPE1."'",$where); 
        }
    }
    return $where;
}

A few things I can think of to check: 我可以考虑检查的几件事:

If you're concerned about time zones being incorrect, you can always have MySQL supply the datetime. 如果您担心时区不正确,可以始终让MySQL提供datetime。 You would change a statement like this: 您将更改如下语句:

$today = date('Y-m-d G:i:s');
$where = "AND date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <='" . $today . "'))";

to something like this: 像这样:

$where = "AND date_format($wpdb->postmeta.meta_value,'%Y-%m-%d %G:%i:%s') <= NOW() ))";

NOW() is MySQL's command to give the current server datetime. NOW()是MySQL提供当前服务器日期时间的命令。

When you're entering the start and end dates into the custom fields, are you using the date or the date and time? 在自定义字段中输入开始日期和结束日期时,您使用的是日期还是日期和时间? If you're not using a time, a date such as "2012-12-31" would get interpreted as "2012-12-31 00:00:00". 如果您不使用时间,则诸如“ 2012-12-31”之类的日期将被解释为“ 2012-12-31 00:00:00”。 That could cause problems, especially with an end date. 这可能会引起问题,尤其是结束日期。 Consider adding a day (or 86,400 seconds) to the end date of this is the case. 考虑将这种情况的结束日期添加一天(或86,400秒)。

If you suspect you're having problems interpreting dates properly, MySQL has an equivalent to PHP's strtotime() command: STR_TO_DATE . 如果您怀疑在正确解释日期时遇到问题,则MySQL与PHP的strtotime()命令等效: STR_TO_DATE You can take a human readable string and convert it to exactly the format you want. 您可以采用人类可读的字符串,并将其完全转换为所需的格式。

When using $wpdb for queries, you can try outputting $wpdb->last_query to see exactly when the full SQL sent to MySQL is. 使用$ wpdb进行查询时,可以尝试输出$ wpdb-> last_query来查看发送到MySQL的完整SQL的确切时间。 Try running that in your favorite MySQL client and see what results you get back. 尝试在您最喜欢的MySQL客户端中运行该命令,然后查看返回的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按高级自定义字段中输入的日期(日期选择器)对Wordpress帖子进行排序 - Sorting Wordpress posts by date entered (Date Picker) in advanced custom fields 使用自定义字段过滤 WordPress 帖子 - Filter WordPress posts with custom fields 只想在 WordPress 前端显示独特的自定义帖子类型帖子 - Wants to show only the unique custom posts type posts in WordPress front end 从MySql中抓取当前日期在开始日期和结束日期之间的行(检查当前日期是否在开始日期和结束日期之间) - Grabbing rows from MySql where current date is in between start date and end date (Check if current date lies between start date and end date) 如果日期之间的当前日期是两个,Wordpress将显示不同的元素 - Wordpress show different element if current date is two between dates WordPress-显示和分层自定义字段/帖子 - Wordpress- Displaying and layering custom fields/posts 根据Wordpress中的自定义字段对帖子进行样式设置? - Style posts based on custom fields in Wordpress? 按数字自定义字段对WordPress帖子进行排序 - Sort WordPress posts by numerical custom fields WordPress 一次通过 2 个自定义字段订购帖子 - WordPress order posts by 2 custom fields at once 将高级自定义字段添加到Wordpress中的相关帖子 - Adding Advanced Custom Fields to Related Posts in Wordpress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM