将日期保存在`meta_key`中的旧事件从WP_QUERY中排除

时间:2017-08-30 作者:Iurie

我使用下面的模板显示由第三方插件创建的自定义帖子类型事件的上升列表中的第一个事件。我想要的是排除旧事件,只显示一个实际事件(如果今天有),或者最近的事件。事件开始日期保存在meta_key 具有“Y-m-d”格式的值(例如:2017-10-27)。如何做到这一点?

<?php

    wp_reset_query();

    $args = array(
        \'post_type\'         => \'events\',
        \'posts_per_page\'    => 1,
        \'meta_key\'          => \'event_start_date\',
        \'order\'             => \'ASC\'
    );

    $featured = new WP_Query($args);

    if ($featured->have_posts()): 
        while($featured->have_posts()): $featured->the_post();

            $featured_event_start = $post->event_start_date;
            $featured_event_end = $post->event_end_date;

            if (has_post_thumbnail()) : ?>
                <div class="featured-event-outer">
                    <div class="featured-event-inner">
                        <figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(\'medium_large\'); ?></a></figure>
                        <h3 class="featured-event-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <h3 class="featured-event-date">
                            <a href="<?php the_permalink(); ?>"><?php echo $featured_event_start;
                                if ( $featured_event_end && $featured_event_end != $featured_event_start ) { echo \' - \' . $featured_event_end; } ?>
                            </a>
                        </h3>
                    </div>
                </div>
            <?php endif; ?>

        <?php endwhile;
            else: ?>
                <!-- Placeholder content goes here -->
    <?php endif;

    wp_reset_postdata();
?>

3 个回复
最合适的回答,由SO网友:Iurie 整理而成

好的,在@jonathan-wold, 我想出了解决问题的办法。在我的初始代码中,我只是替换了$args 部分,因此提到的作者,这就像一个魅力。代码如下:

$args = array(
    \'post_type\' => \'events\', // Tell WordPress which post type we want
    \'orderby\' => \'meta_value\', // We want to organize the events by date    
    \'meta_key\' => \'event_start_date\', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
    \'order\' => \'ASC\', // ASC is the other option    
    \'posts_per_page\' => \'1\', // Let\'s show only one / the first event.
    \'meta_query\' => array( // WordPress has all the results, now, return only the events after today\'s date
        array(
            \'key\' => \'event_start_date\', // Check the start date field
            \'value\' => date("Y-m-d"), // Set today\'s date (note the similar format)
            \'compare\' => \'>=\', // Return the ones greater than or equal to today\'s date
            \'type\' => \'DATE\' // Let WordPress know we\'re working with date
        )
    )
);

SO网友:Abdul Awal Uzzal

您需要以下内容:

$args = array(
    \'post_type\'         => \'events\',
    \'posts_per_page\'    => 1,
    \'meta_query\'        => array(
        \'meta_key\'          => \'event_start_date\',
        \'type\'              => \'DATETIME\',  // You can also try changing it to TIME or DATE if it doesn\'t work
        \'meta_value\'        => date( "Y-m-d" ),
        \'meta_compare\'      => \'>\',
    ),
    \'order\'             => \'ASC\'
);

SO网友:Samad

更新:在“meta\\u key”中将event\\u start\\u date替换为\\u event\\u start\\u date后,代码对我有效

$args = array(
    \'post_type\' => \'events\', // Tell WordPress which post type we want
    \'orderby\' => \'meta_value\', // We want to organize the events by date    
    \'meta_key\' => \'_event_start_date\', // Grab the "start date" field created via "More Fields" plugin (stored in YYYY-MM-DD format)
    \'order\' => \'ASC\', // ASC is the other option    
    \'posts_per_page\' => \'1\', // Let\'s show only one / the first event.
    \'meta_query\' => array( // WordPress has all the results, now, return only the events after today\'s date
        array(
            \'key\' => \'_event_start_date\', // Check the start date field
            \'value\' => date("Y-m-d"), // Set today\'s date (note the similar format)
            \'compare\' => \'>=\', // Return the ones greater than or equal to today\'s date
            \'type\' => \'DATE\' // Let WordPress know we\'re working with date
        )
    )
);

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post