dongnuo4594 2017-05-02 15:59
浏览 78
已采纳

如何仅列出WP_query中的未来事件?

I have a WP-query that should show up to 20 posts of future events. The posts should be only from the post-format-aside and should be ordered by the date. That part works quite well. What does not work, that is to exclude events that have already passed. ( 'compare' => '>=', or 'meta_compare' => '>=', don't work. :( )Could anybody tell me, where is my mistake?

Thanks a lot! rabo

Here is my code:

                    <?php

$current_month = "";

setlocale(LC_ALL, 'de_DE@euro'); // Monatsnamen auf Deutsch ausgeben

$args = array( 'showposts' => 20,
                'cat' => array  
                            (1,2,7,8,9
                            ),
                'meta_key' => 'datumsfeld',
                'orderby' => 'meta_value',
                'compare' => '>=',
                'tax_query' => array(
                                     array(
                                        'taxonomy' => 'post_format',
                                        'field' => 'slug',
                                        'terms' => 'post-format-aside',
                                       )
                                )
                );

$myQuery = new WP_Query( $args   );
while ($myQuery->have_posts()) : $myQuery->the_post();

$datefield = DateTime::createFromFormat('Ymd', get_field('datumsfeld'));
    if($datefield){
        $month = $datefield->format("m");
        $year = $datefield->format("Y");

        if($month != $current_month){
            echo '<div class="abstand"><strong>'.strftime("%B %Y",$datefield->getTimestamp()).'</div></strong>';
            $current_month = $month;
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • duannian4784 2017-05-02 16:16
    关注

    How about adding a meta query in order to exclude unwanted posts? The meta_key you are using is only for ordering the posts correctly.

    Something like:

    $args = array( 'showposts' => 20,
                    'cat' => array  (1,2,7,8,9),
                    'meta_key' => 'datumsfeld',
                    'orderby' => 'meta_value',
                    'compare' => '>=',
                    'tax_query' => array(
                                         array(
                                            'taxonomy' => 'post_format',
                                            'field' => 'slug',
                                            'terms' => 'post-format-aside',
                                           )
                                    ),
                      'meta_query' => array(
                         array(
                            'key'     => 'datumsfeld',
                            'value'   => '...' /* something like today */,
                            'compare' => '>=',
            ),
        ),
                    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。