【问题标题】:Wordpress — display events (custom post type) for the current week (from monday to sunday included)Wordpress — 显示本周的事件(自定义帖子类型)(包括从周一到周日)
【发布时间】:2020-10-14 20:23:40
【问题描述】:

我正在开发一个足球俱乐部的 wordpress,我想显示本周的事件(比赛)。

我必须按名为“date_match”的 ACF 字段而不是帖子本身的日期进行排序,但它不起作用。

匹配是自定义帖子类型。

这是我的查询

<?php
    //define args
    $args = array(
        'post_type'         => 'matchs',
        'orderby'           => 'meta_value',
        'meta_key'          => 'date_match',
        'order'             => 'ASC',
        'posts_per_page'    => 4,
        // Using the date_query to filter posts from last week
        'meta_query' => array(
            array(
                'key' => 'date_match',
                'year' => date( 'Y' ),
                'week' => date( 'W' )
            )
        )

    );
    //query
    $the_query = new WP_Query( $args );

    //loop
    if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
?>
…
<?php endwhile; ?>

<?php else: ?>

    <!-- article -->
    <article>
        <h6>No match to display.</h6>
    </article>
    <!-- /article -->

<?php endif; ?>

这是我在 ACF 中的配置:

【问题讨论】:

    标签: wordpress date advanced-custom-fields custom-post-type


    【解决方案1】:

    你可以试试这样的:

    $start = 'define your start date';
    $end = 'define your end date';
    
    // update your meta_query to search for posts between start and end
    'meta_query' => array(
        array(
            'key' => 'date_match',
            'value' => array($start, $end),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        )
    

    【讨论】:

    • 你好 Mikerojas !谢谢你的提示。它带我走上正确的道路。
    【解决方案2】:
    <?php
    //define args
    //timestamp used to format the date
    $thisMonday = strtotime('this week');
    $thisFriday = strtotime('+6 days', $thisMonday);
    
    $thisMonday = date('Ymd', $thisMonday);
    $thisFriday = date('Ymd', $thisFriday);
    
    $args = array(
        'post_type'         => 'matchs',
        'orderby'           => 'meta_value',
        'meta_key'          => 'date_match',
        'order'             => 'ASC',
        'posts_per_page'    => 4,
        // Using the date_query to filter posts from this week
        'meta_query' => array(
            array(
                'key' => 'date_match',
                'value' => $thisMonday,
                'compare' => '>='
                ),
            array(
                'key' => 'date_match',
                'value' => $thisFriday,
                'compare' => '<='
            )
        )
    
    );
    //query
    $the_query = new WP_Query( $args );
    
    //loop
    if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
    

    ?>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      相关资源
      最近更新 更多