我从wordpress网站上关注本教程:我需要获得所有标题与LIKE子句匹配的帖子%post_title%
注意:post_author返回一个id不是作者姓名,哪个表包含作者用户表的数据?我这样做是正确的吗?
$querystr = "
SELECT post_date,post_title,comment_count,post_excerpt,guid,post_author
FROM $wpdb->posts
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_title LIKE '%$cat[0]%'
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
然后它说您可以使用以下方式获取作者姓名:
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<?php the_author() ?
但作者是空的。
var_dump:
array(2) {
[0]=> object(stdClass)#352 (6)
{ ["post_date"]=> string(19) "2015-05-07 03:15:50"
["post_title"]=> string(33) "The moon"
["comment_count"]=> string(1) "0"
["post_excerpt"]=> string(0) ""
["guid"]=> string(26) "url"
["post_author"]=> string(1) "1" }
[1]=> object(stdClass)#285 (6)
{
["post_date"]=> string(19) "2015-03-31 21:16:25"
["post_title"]=> string(36) "The moon Part 2 More moon"
["comment_count"]=> string(1) "1"
["post_excerpt"]=> string(26) "something about the moon."
["guid"]=> string(24) "url" ["post_author"]=> string(1) "1" } }
答案 0 :(得分:0)
可以使用以下代码获得帖子作者:
foreach ($pageposts as $post) {
$author = get_user_by('id', $post->post_author);
echo $author->display_name;
}