編輯:
除了解決方案概述波紋管,如果你使用WordPress> = 3.5(因爲你應該:)),你可以簡單地使用的WP_Post對象的魔術方法。
基本上,WP_Post對象(這是來自WP_Query的幾乎每個查詢結果的帖子數組由什麼組成)是使用PHP的__get()
和__isset()
魔術方法。這些方法允許您使用對象本身未定義的對象的屬性。
下面是一個例子。
foreach ($posts as $key => $post) {
// This:
echo $post->key1;
// is the same as this:
echo get_post_meta($post->ID, 'key1', true);
}
如果你犯了一個print_r($post)
或var_dump($post)
,你將不會看到$post
對象的「KEY1」屬性。但功能__get()允許您訪問該屬性。
============================================== =============
在我看來,你有兩個常規選項 - 循環訪問並獲取所需的數據,如下所示(此代碼將在$posts = query_posts($args);
之後):
foreach ($posts as $key => $post) {
$posts[ $key ]->key1 = get_post_meta($post->ID, 'key1', true);
$posts[ $key ]->key2 = get_post_meta($post->ID, 'key2', true);
}
或鉤到the_posts
過濾器鉤和做同樣的事情有(更多的工作,但如果你有需要的數據添加到每個崗位多種功能 - 這可能是更容易)。此代碼會去你的functions.php或插件的文件(如果你正在做一個插件):
function my_the_posts_filter($posts) {
foreach ($posts as $key => $post) {
$posts[ $key ]->key1 = get_post_meta($post->ID, 'key1', true);
$posts[ $key ]->key2 = get_post_meta($post->ID, 'key2', true);
}
return $posts;
}
然後你將你的
$posts=query_posts($args);
行改成這樣:
add_filter('the_posts', 'my_the_posts_filter', 10);
$posts = query_posts($args);
remove_filter('the_posts', 'my_the_posts_filter', 10);
考慮到這會發生在AJAX請求中,您可以在技術上擺脫remove_filter()
調用,但是如果您打算進行其他任何後續查詢在你的代碼中。
http://codex.wordpress.org/Class_Reference/WP_Query去那裏尋找'meta_query' – codepixlabs