自定义POST类型的自定义循环。是否按元值进行比较?

时间:2012-12-08 作者:mathiregister

我有点困惑。我有一个名为wr_event.我正在使用下面的代码循环所有“即将到来的”事件(由meta\\u键\'event\\u date定义)。

我现在的问题是,我不仅要显示所有即将发布的帖子,还要显示今年的所有帖子。

if ( have_posts() ): the_post();

    $current_year = mktime(0, 0, 0, 1, 1, date(\'Y\'));
    //$last_year = mktime(0, 0, 0, 1, 1, date(\'Y\') - 1);

    // Show all upcoming events of the current year
    /*$args = array(
        \'post_type\' => \'wr_event\',
        \'posts_per_page\' => -1,
        \'meta_key\' => \'event_date\',
        \'orderby\' => \'meta_value_num\',
        \'order\' => \'DSC\',
        \'meta_value\' => $current_year,
        \'meta_compare\' => \'>\',
        \'taxonomy\' => \'event_type\',
        \'term\' => get_queried_object()->slug
    );*/

    // Show all upcoming and taken place events of the current year
    $args = array(
        \'post_type\' => \'wr_event\',
        \'posts_per_page\' => -1,
        /*\'meta_key\' => \'event_date\',
        \'orderby\' => \'meta_value_num\',
        \'order\' => \'DSC\',
        \'meta_value\' => $last_year,
        \'meta_compare\' => \'>\',*/
        \'taxonomy\' => \'event_type\',
        \'term\' => get_queried_object()->slug
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( \'inc/event\', \'item\' );
    endwhile;
    wp_reset_postdata();

    else: ?>

    <p class="wrapper no-items">
            There are no posts found
        </p>
你知道怎么做吗?我只想列出按当前\\u年的当前分类术语筛选的所有事件。我似乎不知道怎么做?

UPDATE

谢谢……但这看起来怎么样?

 $args = array(
                \'post_type\' => \'wr_event\',
                \'posts_per_page\' => -1,
                \'meta_query\' => array( array(
                    \'key\' => \'event_date\',
                    \'value\' => $current_year,
                    \'compare\' => \'>=\',
                    \'type\' => \'numeric\'
                ) ),
                \'taxonomy\' => \'event_type\',
                \'term\' => get_queried_object()->slug
            );

1 个回复
最合适的回答,由SO网友:Matthew Boynes 整理而成

看看您(在您的注释代码中)尝试了什么,我认为您已经接近了。我要孤注一掷,猜测你的event_date 元字段不会存储为数字时间戳,这是您要比较的内容。假设它存储为文本日期,如“2012-12-08”(如果不是,可能应该是),下面是您应该添加到$args 阵列:

\'meta_query\' => array( array(
    \'key\' => \'event_date\',
    \'value\' => date( \'Y-m-d\', $current_year ),
    \'compare\' => \'>=\',
    \'type\' => \'date\'
) )
编辑:现在知道您的数据存储为numeric unix timestamp, 尝试以下操作:

\'meta_query\' => array( array(
    \'key\' => \'event_date\',
    \'value\' => $current_year,
    \'compare\' => \'>=\',
    \'type\' => \'numeric\'
) )
*注意:旧答案留给将来可能将日期存储为字符串的读者。

结束