忽略今天PHP之前的帖子

时间:2014-11-11 15:23:35

标签: php wordpress

我在Wordpress中有以下循环,即使它始终有效(至少,我认为是这样),它最近停止了工作:

<?php
    $items = 0;
    $thiscat = get_category(3);
    $cat_slug = $thiscat->slug;
    $args = array( 
    'post_type' => 'Course', 
    'posts_per_page' => 3,
    'meta_key' => 'date_start',
    'orderby' => 'meta_value',
    'category_name' => $cat_slug,
    'order' => 'ASC',
); ?>
<ul>
    <?php
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() AND $items < 3) {
        $loop->the_post();              
        $category_course = get_the_category(3);                 
        global $post;
        $category_detail = get_the_category( $post->ID );
        $date_start = get_post_meta(get_the_ID(), 'date_start', true);
        $place = get_post_meta(get_the_ID(), "place", true);
        if( $date_start >= strtotime('today') ) { ?>
        <li>                            
            <a href="<?php the_permalink(); ?>" class="date_fp"><?php echo strftime("%a, %e. %b. %Y", $date_start); ?> - <?php echo $place;?></a>
                <?php foreach ($category_detail as $category_ID)
                    {
                        if($category_ID->cat_name == 'z_aflyst')
                        {
                            echo "- <strong>Aflyst</strong>";
                        }
                }?>
            </li>
            <?php $items++; }
            }
            if($items==0) { ?>
                <li>                            
                    Ingen kommende kurser
                </li>
    <?php } ?>

预期结果:计算将来要举办的所有课程,首页最多显示3个

结果:计算数据库中的所有课程(过去和现在),最多3个,显示在首页上保留的课程(过去持有的课程)不显示,但计算在内)。如果过去举办过3门或以上的课程,则不会显示将来举办的任何课程。

在我的脑海中,它应该忽略所有发布日期today之前的帖子,但它显然仍在计算它们,因为首页上的输出只有一个过程(在上述循环的情况下) ,过去有两个课程,未来有三个课程,而不是可用的3.我发现如果我将posts_per_page更改为4,它会显示还有一门课程,所以它实际上与它在post中将posts_per_page中的课程计算在一起的事实有关。

这可能只是我的代码的一小部分调整,但我如何以及在哪里确保忽略strotime('today')之前的帖子?

1 个答案:

答案 0 :(得分:0)

通过调整我的$args meta-query,我设法将meta-key的值与我选择的值(在我的情况下为time())进行比较。< / p>

通过添加此功能,甚至可以删除我的if - 句子,我会查看将来是否放置课程。

$args['meta_query'] = array(
    array(
        'key' => 'date_start',
        'value' => time(),
        'compare' => '>='
        )
    )

这导致$args变量仅抓取等于或大于time()的帖子。借助StackOverflow的Epic解决方案,因为它甚至使我的代码更具可读性。