【问题标题】:Wordpress - get posts ordered by meta valueWordpress - 按元值获取帖子顺序
【发布时间】:2013-08-01 08:26:49
【问题描述】:

我尝试按元值从 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”的元键。这可以正常工作 - 我也查看了数据库并且元密钥正在正确更新。如果帖子是 TOP,则它的元键值为 1,如果不是,则此帖子没有名为“top”的元键。

那么我怎样才能用这样的元键来订购帖子呢?

谢谢。

【问题讨论】:

    标签: wordpress meta


    【解决方案1】:

    这对我有用:

    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 -->
    

    【讨论】:

    • 这不起作用,因为没有名为 'top' 的元键的帖子不计算在内。我需要获取所有带有或不带有名为“top”的元键的帖子,然后按元值排序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多