Wordpress - 获取按元值排序的帖子

时间:2013-08-01 08:26:49

标签: wordpress meta

我尝试通过元值从wp数据库顺序获取帖子。 问题是有些帖子在数据库中有元键,有些则没有。

我试试这段代码:

$args = array(
        'post_type' => 'post',
        'meta_key' => 'top',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'top',
                'compare' => 'NOT EXISTS',
                'value' => ''
            ),
            array(
                'key' => 'top',
                'value' => '1'
            )
        ),
        'orderby' => '1'
    );
    $posts = new WP_Query($args);

这个节目发布但也没有按元键排序。

为了更好地解释,我希望一些帖子始终显示在顶部或前面。所以我添加名为'top'的元键。这工作正常 - 我也查看过数据库和元键正确更新。如果post是TOP而不是具有值为1的元键,如果没有,则此帖子中没有名为“top”的元键。

那么如何订购具有类似元键的帖子呢?

感谢。

1 个答案:

答案 0 :(得分:0)

这对我有用:

query_posts('meta_key= top&orderby=meta_value');

完整代码:

  <?php 
query_posts('meta_key=facebook_likes&orderby=meta_value');
?>    
<?php while (have_posts()) : the_post(); ?>

<div class="post">
<h2 class="postTitle"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
<div class="postMeta">
<span class="date"><?php the_time('M.d, Y') ?></span> in
<span class="filed"><?php the_category(', '); ?></span>
</div>
<div class="postContent"><?php the_content(); ?></div>
<p class="comments"><?php comments_popup_link(); ?></p>
</div> <!-- Closes Post -->
相关问题