Got the following in my functions file, which is designed to query the last three posts published, however I want to limit it by a certain category.

I was looking at adding is_category to the WHERE declaration but it didnt seem to work.

$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post' ORDER BY post_date DESC LIMIT $no_posts";

1 Answer 1


Instead of a sql query, I recommend to do it in wordpress way:

$args = array(
'numberposts' => 3,
'offset' => 0,
'category' => put_your_category_id_here,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );

$request = wp_get_recent_posts( $args, ARRAY_A );

Reference : WordPress

Update : here is the way to get thumbnail url when you have postid

    $post_thumbnail_id = get_post_thumbnail_id( $post_id );
      $imageurl = wp_get_attachment_url( $post_thumbnail_id );

answer to your last comment :

foreach( $recent_posts as $recent ){ 
    echo '<a href="' . get_permalink($recent["ID"]) . '" rel="bookmark" title="' . $recent["post_title"] . '">
      $post_thumbnail_id = get_post_thumbnail_id($recent["ID"]);
      $imageurl = wp_get_attachment_url( $post_thumbnail_id );
      echo '<img src="' . $imageurl . '" alt="" class="latest-img" >';
      echo '<img src="' . get_template_directory_uri() .'/img/g.png" class="g" width="667" height="266">
      <div class="latest-meta">
        <time class="hstack white">DATE</time>
        <h2 class="white gstack">' . $recent["post_title"] . '</h2>
  • Thanks - I've tried to implement this as it looks cleaner/easy to understand. How would I get that request to output '$posts = $request' ?
    – cubechris
    Aug 18, 2015 at 13:08
  • I am not getting what you are trying to ask? ... if you loop using foreach you can get everything from the posts in result. Aug 18, 2015 at 13:23
  • Sorry that wasn't very clear. I've managed to get it working now using foreach - the only think I'm not sure is how to pull in the thumbnail URL. wp_get_attachment_url($thumbnail)
    – cubechris
    Aug 18, 2015 at 13:29
  • I am adding it in answer. also, if you find answer helpful, you should accept it so that it helps others as well. Aug 18, 2015 at 13:38
  • Thanks Gopal, this is really helpful. How would I add the above code you added to the foreach statement? foreach( $recent_posts as $recent ){ echo '<a href="' . get_permalink($recent["ID"]) . '" rel="bookmark" title="' . $recent["post_title"] . '"><article><img src="' . wp_get_attachment_url($thumbnail) . '" alt="" class="latest-img" ><img src="' . get_template_directory_uri() .'/img/g.png" class="g" width="667" height="266"><div class="latest-meta"><time class="hstack white">DATE</time><h2 class="white gstack">' . $recent["post_title"] . '</h2></div></article></a>'; }
    – cubechris
    Aug 18, 2015 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.