如何用相同的帖子元获得所有帖子?

时间:2011-05-28 作者:Jenny

我的一些帖子有“project\\u id”的帖子元。如何检索所有具有“project\\u id”元键的帖子?我试过:

$meta_key = \'project_id\';
return $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));
它不返回任何内容。请帮忙!谢谢

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

就这么简单:$the_posts_you_want = get_posts( array( \'meta_key\' => \'project_id\' ) );

foreach ( $the_posts_you_want as $post )
{
    // Do whatever you need in here... 
    // Read further how to inspect the post object: http://wordpress.stackexchange.com/questions/13063/how-to-inspect-global-variables-in-wordpress
    // echo each meta key
    echo $post->whatever;
    // or save it into a new array for further processing
    $project_ids = array();
    $project_ids[] = $post->whatever;
}
// If you saved them into an array, you can continue here...
foreach ( $project_ids as $id )
{
    // do stuff
}

SO网友:Bainternet

由于没有字段,因此查询不返回任何内容ID 在Posteta表中,所以它真的应该是post_id:

$meta_key = \'project_id\';
return $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key));

结束

相关推荐