存档页面上的事件列表,带有自定义帖子类型

问题描述 投票:0回答:2

我正在使用Wordpress。我想显示即将发生的事件,这些事件是自定义帖子类型模板中的帖子(posttype-archive.php)。我想将自定义字段与unix timestam与当前时间戳进行比较。不知何故,它始终列出此特定自定义帖子类型的所有帖子。

<?php
$today = time();

//First Query for Posts matching term1
$args = array(
    'post_type' => 'exhibitions',
    'relation' => 'AND',
    'meta_query'    => array(  
                   'key'     => 'start_date',  
                   'value'   => $today, 
                   'compare' => '>=',  
                   'type'    => 'NUMERIC'
              )

);
$query = new WP_Query( $args );



if ( have_posts() ) {


    $term = $query->queried_object;

    while ( have_posts() ) : the_post(); ?>

    <?php  ?>

    <?php print_r ($args); ?>

    <div class="auss_left">
        <h2><?php echo rwmb_meta( 'gastgeberin' ); ?><br />
        <?php the_title(); ?></h2>

        <?php 
        $date =rwmb_meta( 'Date' );
        if (!$date){
        echo rwmb_meta( 'start_date' ); ?> - <?php echo rwmb_meta( 'end_date' ); 
        }
        ?>

        <?php if ($date){
            echo rwmb_meta( 'Date' ); 
        } ?>


        <?php echo rwmb_meta( 'Ort' ); ?>
        <?php echo rwmb_meta( 'Adresse' ); ?>

        <?php echo rwmb_meta( 'Karte' ); ?>

        Öffnungszeiten:<br/>
        <?php echo rwmb_meta( 'oeffnungszeiten' ); ?>

        Vernissage:<br/>
        <?php echo rwmb_meta( 'vernissage' ); ?>


    </div>

    <div class="auss_right">
        <?php the_content(); ?>
    </div>


        <?php endwhile;
}

//RESET YOUR QUERY VARS
wp_reset_query();

 ?>

我希望此WP查询仅列出即将发生的事件,但它始终列出所有事件。

wordpress meta-boxes
2个回答
0
投票

我找到了一个解决方案:https://wordpress.stackexchange.com/questions/313355/use-meta-query-to-display-events-by-date-in-custom-field

我做到了这样,它工作,我认为这只是阵列结构的差异,我使用YYYY-MM-DD格式。


0
投票

您正在创建自定义WP_Query,但您没有在任何地方使用它。试试这个:

<?php
$today = time();

//First Query for Posts matching term1
$args = array(
    'post_type'  => 'exhibitions',
    'relation'   => 'AND',
    'meta_query' => array(
        array(
            'key'     => 'start_date',
            'value'   => $today,
            'compare' => '>=',
            'type'    => 'NUMERIC',
        ),
    ),
);

$events_query = new WP_Query( $args );

if ( $events_query->have_posts() ) :
    while ( $events_query->have_posts() ) : $events_query->the_post(); ?>
        <div class="auss_left">
            <h2>
                <?php echo rwmb_meta( 'gastgeberin' ); ?>

                <br />

                <?php the_title(); ?>
            </h2>

            <?php
            $date = rwmb_meta( 'Date' );
            if ( ! $date ) {
                echo rwmb_meta( 'start_date' ); ?> - <?php echo rwmb_meta( 'end_date' );
            } else {
                echo rwmb_meta( 'Date' );
            }

            echo rwmb_meta( 'Ort' );

            echo rwmb_meta( 'Adresse' );

            echo rwmb_meta( 'Karte' );
            ?>

            Öffnungszeiten:<br/>

            <?php echo rwmb_meta( 'oeffnungszeiten' ); ?>

            Vernissage:<br/>

            <?php echo rwmb_meta( 'vernissage' ); ?>
        </div><!-- /.auss_left -->

        <div class="auss_right">
            <?php the_content(); ?>
        </div><!-- /.auss_right -->
    <?php endwhile;
endif;

//RESET YOUR QUERY VARS
wp_reset_postdata();

同样如@josedasilva所述,您应该仔细检查为“start_date”元键存储的值是否为整数。

© www.soinside.com 2019 - 2024. All rights reserved.