在一个有序列表中获取今天'下20篇即将发布的文章- Wordpress


Get the present day's next 20 upcoming posts in a ordered list - Wordpress

我发布的帖子包含即将发生的事件的信息。

我有一个高级自定义字段称为category-upcoming-date用于这些帖子。该字段的格式为yy-mm-dd

我提出了以下解决方案:

<?php 
// get posts
$posts = get_posts(array(
            'post_type'         => 'post',
            'category_name'     => 'category-upcoming',
            'posts_per_page'    => 20,
            'meta_key'          => 'category-upcoming-date',
            'orderby'           => 'category-upcoming-date',
            'order'             => 'DESC'
));
if( $posts ): ?>
    <?php foreach( $posts as $post ): 
            setup_postdata( $post )
            ?>
            <h4>
            <a href="<?php the_permalink(); ?>"><?php the_field('category-upcoming'); ?> <?php the_title(); ?></a>
            </h4>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>

例如,这给了我一个看起来像这样的列表:

2018-12-24 Christmas
2017-05-18 Yoga
2015-07-31 Outdoor 
2015-04-03 Climbing
2015-01-01 Running
2013-04-02 Swimming
2012-08-22 Superbowl

我的问题是我不想看到category-upcoming-date比现在更老的帖子。所以2013-04-02游泳2012-08-22超级碗不应该被看到,但他们是。

如果有40个即将到来的post事件,我只想看到接下来的20个

我发现了这个:

<?php 
$blogtime = current_time( 'mysql' ); 
list( $today_year, $today_month, $today_day ) = split( '([^0-9])', $blogtime );
?>

但不知道如何使用它。

你的答案是在WordPress文档中使用offset索引:

$args = array( 'posts_per_page' => 20, 'offset'=> 20, 'category' => 1 );
$myposts = get_posts( $args );

我认为您可能首先使用get_field('category-upcoming-date')从特殊字段检索您的发布日期。

然后你可以这样做,使用strcmp()对两个字符串进行比较:

// Note the format: You can use this since 3.0.9 and we only want the date
$today = current_time('Y-m-d'); 
$postdate = get_field('category-upcoming-date');
if(strcmp($postdate, $today) >= 0) {
   // If it´s larger or equal 0, the first string is equal or larger as the second
}

更新:关于这个主题,这也是最快的解决方案。所以你可以试试