基于日期参数的If语句


if statement based on date paramater

我有一个从自定义帖子类型获取帖子的wordpress查询。帖子有一个自定义的元日期字段。我想把有一个数据在一个名为"正在进行"表的未来的帖子(这部分是完整的,似乎工作得很好)。我想把一个日期在过去的帖子在一个名为"发布"的表。这就是我有问题的地方,因为if语句似乎显示帖子,即使他们有一个日期是在未来。我使用如果完成日期等于或大于开始时间,现在。对于正在进行的职位。我用相反的方式发表的帖子。看到它是相同的,但倒置,我不明白为什么查询从未来的日期拉帖子。

我做错了什么?

提前感谢


正在工作

<?php $wp_query_ongoing = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
            <?php while ( $wp_query_ongoing->have_posts() ) : $wp_query_ongoing->the_post(); //start of the loop ?>
        <?php
            $post_id = get_the_ID();
            $completion_date = get_post_meta( $post_id, "duration_end", true );
            $publication_date = get_post_meta( $post_id,  "publication_date", true );
        ?>
            <?php if (strtotime($publication_date) >= strtotime("now") || strtotime($completion_date) >= strtotime("now")) { ?>
            <tr>
                <td><?php echo do_shortcode('[wpuf-meta name="_hidden_type"]' ); ?></td>
                <td><?php the_title(); ?></td>
    ...
                <td>
                    <?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
                    <?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
                </td>
            </tr>
            <?php } ?>
        <?php endwhile; //end of the loop ?>

已发布-不工作

<?php $wp_query_published = new WP_Query( array( 'post_type' => 'research', 'posts_per_page' => 5, 'order' => 'asc' ) ); ?>
        <?php while ( $wp_query_published->have_posts() ) : $wp_query_published->the_post(); //start of the loop ?>
        <?php
            $post_id = get_the_ID();
            $completion_date = get_post_meta( $post_id, "duration_end", true );
            $publication_date = get_post_meta( $post_id,  "publication_date", true );
        ?>
            <?php if ( strtotime($publication_date) <= strtotime("now") || strtotime($completion_date) <= strtotime("now")){ ?>
<td>
                    <?php echo do_shortcode('[wpuf-meta name="duration_end"]'); ?>
                    <?php echo do_shortcode('[wpuf-meta name="publication_date"]'); ?>
                </td>
<?php } ?>
        <?php endwhile; //end of the loop ?>

仔细考虑条件。您显示的已发布的内容与您显示的正在进行的内容并不完全相反。

试试这个:

if(strtotime($publication_date) < strtotime("now") && strtotime($completion_date) < strtotime("now"))

请注意,条件的每个部分都与正在进行的条件(我删除了=)中的内容完全相反。还要注意,在这种情况下,您需要使用AND来连接这些条件,因为如果您说任何一个条件必须为真才能使项目进入正在进行的类别,那么必须不满足两个条件才能进入已发布的类别。

根据你的评论,你可能想做这样的比较:

$comparison_date = get_post_meta( $post_id, "duration_end", true );
if (empty($comparison_date )) {
    $comparison_date = get_post_meta( $post_id,  "publication_date", true );
}
$comparison_date = strtotime($comparison_date);
// use this for ONGOING
if ($comparison_date >= time()) {
   ...
}
// use this for PUBLISHED
if ($comparison_date < time()) {
   ...
}

您还应该强烈考虑将其重构为一个可以处理两种过滤情况的脚本。没有理由复制所有这些代码。