2013-08-01 17 views
1

我嘗試通過元值從wp數據庫順序獲取帖子。 問題是有些帖子在數據庫中有元鍵,有些不是。WordPress的 - 獲取按元值排序的帖子

我試試這個代碼:

$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」的元鍵。

那麼我怎樣才能訂購與這樣的元鍵職位?

謝謝。

回答

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

這不是工作,因爲沒有名爲「頂」元關鍵崗位不計。我需要獲取所有帶有或沒有名爲'top'的元鍵的帖子,然後按元值排序。 – quarky