1

Hi I want to display a specific category from a custom query in WordPress. My code works fine and it gets the latest 4 posts, but what I want now is to retrieve a specific category. My code is below thanks :

global $wpdb;

$posts = $wpdb->get_results('SELECT ID, post_title AS title, post_excerpt AS excerpt FROM '.$wpdb->posts.' WHERE post_type = "post" AND post_status = "publish" ORDER BY post_date DESC LIMIT 4');

And here is my full code :

global $wpdb;


$posts = $wpdb->get_results('SELECT ID, post_title AS title, post_excerpt AS excerpt FROM '.$wpdb->posts.' 
 WHERE  post_type = "post" AND post_status = "publish" ORDER BY post_date  cat = "category_id" DESC LIMIT 4');

$items = array();
foreach ($posts as $post) {


    $item = array();
    $item['title'] = get_the_title($post->ID);
    $item['url'] = get_permalink($post->ID);
    $item['desc'] = $post->excerpt;
    $item['image'] = wp_get_attachment_url( get_post_thumbnail_id($post->ID));
    $item['thumb'] = get_post_meta($post->ID, 'Thumbnail', true);
    $items[] = $item;
}

return $items;

Now i want to add the final out pout see the code below :

" id="nav-fragment-"> "> " class="ui-tabs-panel" style=""> " alt="" /> " >

" >Read more

5
  • It is highly recommended to use wp_query to fetch WordPress posts. Check this Commented Sep 30, 2014 at 8:44
  • @Nilambar i used that but no luck mind helping
    – Jono
    Commented Sep 30, 2014 at 8:45
  • did u mean "displaying posts of specific category" ??
    – Zammuuz
    Commented Sep 30, 2014 at 8:46
  • yes that what i want
    – Jono
    Commented Sep 30, 2014 at 8:50
  • Here is my classic answer: wordpress.stackexchange.com/questions/93798/… Commented Sep 30, 2014 at 9:04

2 Answers 2

3

the recommended way is:

    <?php
    $query = new WP_Query('category_name=Category&posts_per_page=4');
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
    if (has_post_thumbnail()) {
        ?>
            <a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
        <?php
    }
    ?>
    <h2><a href="<?php the_permalink(' ') ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php
    the_excerpt(); // or the_content(); for full post content
    endwhile;endif;
?>
0
3

All good, the very first suggestion from @Ammu was also working. I needed to change the values from the array to match the statement here is the code:

global $wpdb;
$cat_id = 10; 

$posts = $wpdb->get_results("SELECT 
ID, post_title AS title, post_excerpt AS excerpt FROM $wpdb->posts p
JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id)
JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
JOIN $wpdb->terms t ON (tt.term_id = t.term_id)
WHERE p.post_type='post'
AND p.post_status = 'publish'
AND tt.taxonomy = 'category'
AND t.term_id = $cat_id
ORDER BY post_date DESC LIMIT 4");
2
  • It should be noted that bypassing the API and querying directly may have unintended consequences, you remove the ability to do all of the things the API enables- filters, actions, etc.. You should always use the API (WP_Query) when possible and not a direct SQL query.
    – Milo
    Commented Sep 30, 2014 at 16:55
  • What if i want to display 2 specific categories ?
    – Jono
    Commented Dec 10, 2014 at 9:53

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.