简体   繁体   中英

wordpress get posts LIKE with author

I followed this tutorial from a wordpress site: I need to get all posts which title matches the LIKE clause %post_title%

Note: post_author returns an id not the authors name, which table contains data on the author the user table? Am i doing this the right way?

 $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);

It then says you can get author name using:

 <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
           <?php the_author() ?

But the author is empty.

the 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" } }

Post author could be obtained using the following code:

foreach ($pageposts as $post) {
    $author = get_user_by('id', $post->post_author);
    echo $author->display_name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM