【发布时间】:2015-04-26 21:28:43
【问题描述】:
有人可以帮我显示特定自定义帖子类型的所有帖子和 Postmeta
我在 wordpress 项目中创建了一个自定义帖子类型“课程”,其中包含一些自定义元框
我需要显示该自定义帖子类型的所有帖子和自定义元框值
我已经尝试了下面的查询。
$query = "
SELECT $wpdb->posts.ID,$wpdb->posts.post_title,$wpdb->postmeta.meta_key
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE
$wpdb->postmeta.meta_key IN ('_course_code','_instructor_name')
AND
$wpdb->posts.post_type = 'course'
";
$results = $wpdb->get_results($query);
echo "<pre>";
print_r($results);
通过对这个查询结果进行分组,它只给出一个 meta_key 值
它给了我结果:
请注意 id 和 post_title 以新索引显示两次
Array
(
[0] => stdClass Object
(
[ID] => 10
[post_title] => introduction to wordpress
[meta_key] => _course_code
)
[1] => stdClass Object
(
[ID] => 10
[post_title] => introduction to wordpress
[meta_key] => _instructor_name
)
[2] => stdClass Object
(
[ID] => 13
[post_title] => introduction to hacking
[meta_key] => _course_code
)
[3] => stdClass Object
(
[ID] => 13
[post_title] => introduction to hacking
[meta_key] => _instructor_name
)
)
但我期待结果为
Array
(
[0] => stdClass Object
(
[ID] => 10
[post_title] => introduction to wordpress
[meta_key] => _course_code
[meta_key] => _instructor_name
)
[2] => stdClass Object
(
[ID] => 13
[post_title] => introduction to hacking
[meta_key] => _course_code
[meta_key] => _instructor_name
)
)
【问题讨论】:
-
考虑提供与您想要的结果相对应的适当 DDL