$ wp_query代码无法正确显示

时间:2016-11-10 06:07:58

标签: wordpress custom-post-type

我最近从头开始创建一个wordpress电影网站,其中我现在发现很难找出无效的代码。 我遇到的问题是我有一个自定义的帖子类型"电影"和上面提到的帖子类型附加的一些自定义元数据,我想要做的是列出我的自定义帖子类型"电影"按顺序排列元数据"发布"。这是我的functions.php文件的一部分。

add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );

function your_prefix_meta_boxes( $meta_boxes ) {
$meta_boxes[] = array(
    'title'      => __( 'Movie Meta Box', 'textdomain' ),
    'post_types' => 'movies',
    'fields'     => array(
        array(
            'id'   => 'name',
            'name' => __( 'Title', 'textdomain' ),
            'type' => 'text',
        ),
        array(
            'id'   => 'released',
            'name' => __( 'Release Date', 'textdomain' ),
            'type' => 'date',
        ),
        array(
            'id'      => 'rating',
            'name'    => __( 'Rating', 'textdomain' ),
            'type'    => 'radio',
            'options' => array(
                '0' => __( 'Unrated', 'textdomain' ),
                '1' => __( '1', 'textdomain' ),
                '2' => __( '2', 'textdomain' ),
                '3' => __( '3', 'textdomain' ),
                '4' => __( '4', 'textdomain' ),
                '5' => __( '5', 'textdomain' ),
                '6' => __( '6', 'textdomain' ),
                '7' => __( '7', 'textdomain' ),
                '8' => __( '8', 'textdomain' ),
                '9' => __( '9', 'textdomain' ),
                '10' => __( '10', 'textdomain' ),
            ),
        ),
        array(
            'id'   => 'duration',
            'name' => __( 'Duration', 'textdomain' ),
            'type' => 'text',
        ),
        array(
            'id'      => 'genre',
            'name'    => __( 'Genre', 'textdomain' ),
            'type'    => 'textarea',
        ),
        array(
            'id'   => 'director',
            'name' => __( 'Director/s', 'textdomain' ),
            'type' => 'textarea',
        ),
        array(
            'id'   => 'writer',
            'name' => __( 'Writer/s', 'textdomain' ),
            'type' => 'textarea',
        ),
        array(
            'id'   => 'stars',
            'name' => __( 'Star/s', 'textdomain' ),
            'type' => 'textarea',
        ),
        array(
            'id'   => 'storyline',
            'name' => __( 'Storyline', 'textdomain' ),
            'type' => 'textarea',
        ),
    ),
);
return $meta_boxes;
}

这就是archives-movies.php

<?php get_header(); ?>

<?php bd_pagination(); ?>

<div class="row" role="main"><!-- ROW -->

<?php

$args = array(

        "posts_per_page"    => 10,
        "post_type"         => "movies",
        "post_status"       => "publish",
        "meta_key"          => "released",
        "orderby"           => "meta_value_num",
        "order"             => "DESC"

        );

$wp_query = new WP_Query( $args );

?>

<?php if ( $wp_query->have_posts() ) : ?>

<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

<div class="col-lg-9">

<div class="panel panel-default panel-body" style="padding-bottom:0;"><!-- PANEL -->

<p class="fa fa-calendar"></p>&nbsp;<?php the_time( 'd-m-Y' ); ?> | <p class="fa fa-clock-o"></p> <?php the_time( 'H:i a' ); ?> |&nbsp;<p class="fa fa-user"></p>&nbsp;<a href="<?php echo get_author_posts_url(get_the_author_meta( 'ID' )); ?>"><?php the_author(); ?></a></i> |&nbsp;<p class="fa fa-envelope-o"></p>&nbsp;posted in movies

<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?>">

    <div class="post-thumbnail">

        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'image-poster' ); ?></a>

    </div>

    <article class="post-thumbnail-text">

        <h4 class="align_Center" style="color:#000; padding-bottom:1em;">

            <?php the_title(); ?>

        </h4>

        <?php 

            $nam = get_post_meta( get_the_ID(), "name", true );
            $rel = get_post_meta( get_the_ID(), "released", true );
            $rat = get_post_meta( get_the_ID(), "rating", true );
            $dur = get_post_meta( get_the_ID(), "duration", true );
            $gen = get_post_meta( get_the_ID(), "genre", true );
            $dir = get_post_meta( get_the_ID(), "director", true );
            $wri = get_post_meta( get_the_ID(), "writer", true );
            $sta = get_post_meta( get_the_ID(), "stars", true );
            $sto = get_post_meta( get_the_ID(), "storyline", true );

        ?>

        <div class="align_Left">

            <p><b>Title:</b>&nbsp;<?php echo $nam; ?></p>
            <p><b>Released:</b>&nbsp;<?php echo $rel; ?></p>
            <p><b>Rating:</b>&nbsp;<?php echo $rat; ?>/10</p>
            <p><b>Duration:</b>&nbsp;<?php echo $dur; ?>&nbsp;min</p>
            <p><b>Genre:</b>&nbsp;<?php echo $gen; ?></p>
            <p><b>Director/s:</b>&nbsp;<?php echo $dir; ?></p>
            <p><b>Writer/s:</b>&nbsp;<?php echo $wri; ?></p>
            <p><b>Stars:</b>&nbsp;<?php echo $sta; ?></p>

        </div>

    </article>

</article>

<br>

<div class="panel panel-default panel-body" style="padding:20px 20px 10px 20px">

    <p><b>Storyline:</b>&nbsp;<?php echo $sto; ?></p>

</div>

</div><!-- /PANEL -->

</div>

<?php endwhile; ?>

<?php else : echo '<p>NO CONTENT FOUND</p>'; ?>

<?php wp_reset_postdata(); ?>

<?php endif; ?>

</div><!-- /ROW -->

<?php bd_pagination(); ?>

<br>

</div><!-- ./CONTAINER --> 

<?php get_template_part( 'Secondfooter' ); ?>

<?php get_footer(); ?>

希望有人可以帮助我......已经有好几周了。

提前致谢

1 个答案:

答案 0 :(得分:0)

使用meta_type定义正确的类型:

'meta_key'  => 'released',
'meta_type' => 'DATE',
'orderby'   => 'meta_value_date',
'order'     => 'DESC'

(此示例假定使用DATE格式)

  

meta_value - 请注意,查询中还必须包含meta_key=keyname。另请注意,排序将按字母顺序排列,对于字符串(即单词)来说很好,但对于数字可能是意外的(例如1,3,34,4,56,6等,而不是1,3,4,6,正如你自然期望的那样,34,56)。使用meta_value_num代替数字值。如果要将元值转换为特定类型,也可以指定meta_type。可能的值包括NUMERICBINARYCHARDATEDATETIMEDECIMALSIGNEDTIMEUNSIGNED,与$meta_query相同。使用meta_type时,您也可以相应地使用meta_value_ *。例如,将DATETIME用作meta_type时,您可以使用meta_value_datetime来定义订单结构。

在此处详细了解:Orderby_Parameters