我已经创建了几个元框,我想展示与特定元框值相关的帖子。就像我们得到与标签或类别相关的帖子一样。
获得与特定meta box相关的帖子?
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值”更多的信息!结束