pre_get_posts post_meta event

时间:2014-11-06 作者:aifrim

我希望pre_get_posts 筛选中显示的帖子archive-events.phptaxonomy-eventcat.php 模板。

我有一个post_meta 正在使用命名o_event_start 将值存储在DATETIME (2014-12-31 21:30:00)格式,我希望显示所有事件(cpt)

今天(仅在第1页)正在使用WP_QueryWP_Querytoday 或tomorrow (paged)

  • 已经发生了(paged)
  • 3和4需要在主循环中发生,我被指出了错误pre_get_posts 但我迷路了。有人知道怎么做吗?

    我忘了提一下:你是怎么订购的o_event_start (过去下降,未来上升)。

    2 个回复
    SO网友:Marcin

    如果您的存储日期不同,那么时间戳将很难做到这一点。

    要获取此if is timestamp stored,需要添加如下“meta\\u query”:

    add_action(\'pre_get_posts\', \'my_pre_get_posts\');
    function my_pre_get_posts($query)
    {
        $offest = 0;// you must count this to avoid today and tomorrow
        $query->set(
            \'meta_query\',
            array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'o_event_start\',
                    \'value\' => time() + $offest,
                    \'type\' => \'numeric\',
                    \'compare\' => \'>\',
                )
            )
        );
     }
    
    0:01的$OFFET很简单2*24*60*60-1(两天*24小时*60米*60秒-1秒),其他时间必须从当前时间开始。

    SO网友:aifrim

    多亏了马辛,我设法解决了所有的问题。

    function o_pre_get_posts_events($query)
    {
        if (is_post_type_archive("post")) { // I need this for all my other cpts, but not for post
            return;
        }
    
        // we get the day of today
        $today = date("Y-m-d");
        // we add to it (2 days for me)
        $two_days = date("Y-m-d", strtotime($today . " + 2 days"));
    
        // if events are past or future
        $compare = (get_query_var(\'past\') == true) ? "<" : ">";
    
        // order desc for past and asc for future
        $order = (get_query_var(\'past\') == true) ? "desc" : "asc";
        $query->set(
            \'meta_query\',
            array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'o_event_start\',
                    \'value\' => $two_days,
                    \'type\' => \'datetime\',
                    \'compare\' => $compare,
                )
            )
        );
    
        // pagination
        $query->set(\'paged\', get_query_var(\'paged\'));
        $query->set(\'posts_per_page\', 6);
    
        // ordering
        $query->set(\'meta_key\', \'o_event_start\');
        $query->set(\'orderby\', \'o_event_start\');
        $query->set(\'order\', $order);
    }
    
    add_action(\'pre_get_posts\', \'o_pre_get_posts_events\');
    

    结束

    相关推荐

    pre_get_posts post_meta event - 小码农CODE - 行之有效找到问题解决它

    pre_get_posts post_meta event

    时间:2014-11-06 作者:aifrim

    我希望pre_get_posts 筛选中显示的帖子archive-events.phptaxonomy-eventcat.php 模板。

    我有一个post_meta 正在使用命名o_event_start 将值存储在DATETIME (2014-12-31 21:30:00)格式,我希望显示所有事件(cpt)

    今天(仅在第1页)正在使用WP_QueryWP_Querytoday 或tomorrow (paged)

  • 已经发生了(paged)
  • 3和4需要在主循环中发生,我被指出了错误pre_get_posts 但我迷路了。有人知道怎么做吗?

    我忘了提一下:你是怎么订购的o_event_start (过去下降,未来上升)。

    2 个回复
    SO网友:Marcin

    如果您的存储日期不同,那么时间戳将很难做到这一点。

    要获取此if is timestamp stored,需要添加如下“meta\\u query”:

    add_action(\'pre_get_posts\', \'my_pre_get_posts\');
    function my_pre_get_posts($query)
    {
        $offest = 0;// you must count this to avoid today and tomorrow
        $query->set(
            \'meta_query\',
            array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'o_event_start\',
                    \'value\' => time() + $offest,
                    \'type\' => \'numeric\',
                    \'compare\' => \'>\',
                )
            )
        );
     }
    
    0:01的$OFFET很简单2*24*60*60-1(两天*24小时*60米*60秒-1秒),其他时间必须从当前时间开始。

    SO网友:aifrim

    多亏了马辛,我设法解决了所有的问题。

    function o_pre_get_posts_events($query)
    {
        if (is_post_type_archive("post")) { // I need this for all my other cpts, but not for post
            return;
        }
    
        // we get the day of today
        $today = date("Y-m-d");
        // we add to it (2 days for me)
        $two_days = date("Y-m-d", strtotime($today . " + 2 days"));
    
        // if events are past or future
        $compare = (get_query_var(\'past\') == true) ? "<" : ">";
    
        // order desc for past and asc for future
        $order = (get_query_var(\'past\') == true) ? "desc" : "asc";
        $query->set(
            \'meta_query\',
            array(
                \'relation\' => \'AND\',
                array(
                    \'key\' => \'o_event_start\',
                    \'value\' => $two_days,
                    \'type\' => \'datetime\',
                    \'compare\' => $compare,
                )
            )
        );
    
        // pagination
        $query->set(\'paged\', get_query_var(\'paged\'));
        $query->set(\'posts_per_page\', 6);
    
        // ordering
        $query->set(\'meta_key\', \'o_event_start\');
        $query->set(\'orderby\', \'o_event_start\');
        $query->set(\'order\', $order);
    }
    
    add_action(\'pre_get_posts\', \'o_pre_get_posts_events\');
    

    相关推荐