获得与特定meta box相关的帖子?

时间:2014-06-30 作者:Pankaj Kumar

我已经创建了几个元框,我想展示与特定元框值相关的帖子。就像我们得到与标签或类别相关的帖子一样。

1 个回复
最合适的回答,由SO网友:Bysander 整理而成

大致如下:

$args = array(
              \'posts_per_page\' => 10, 
              \'post_type\' => \'post\',
              \'meta_key\' => \'METAkeyNAME\', //what I assume you\'ve called a meta box
              \'meta_value\' => \'THEdesiredVALUE\',
              \'orderby\' => \'ID\', //choose to order by anything look here [http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters] for more info
              \'order\' => \'ASC\',
              );

$results = new WP_Query($args);
//    \'<pre>\'.print_r($results).\'</pre>\'; // This is a useful line to keep - uncomment it to print all results found - it can then be used to work out if the query is doing the right thing or not.
while ($results->have_posts()) {
    $results->the_post();
    the_title();
    the_content();
    echo \'</hr>\'; // puts a horizontal line between results
}

    wp_reset_postdata(); //re-sets back to normal       

}
请注意,这是未经测试的代码,完全取决于您想从中获得什么-需要比“特定的metabox值”更多的信息!

结束

相关推荐

QUERY_POSTS按多种方式排序

我对query\\u posts orderby multiple ways有问题。我在Wordpress中有一个类别,在那里我需要显示具有排名的第一个帖子,然后按标题显示所有帖子。下面是工作代码,工作正常,但不知道如何连接这些代码。这段代码只显示排名靠前的帖子 query_posts(array(\'cat\' => $category->term_id, \'posts_per_page\' => -1, \'orderby\' => \'meta_value\', \'me