Title: Get categories
Last modified: August 19, 2016

---

# Get categories

 *  Resolved [tcushing](https://wordpress.org/support/users/tcushing/)
 * (@tcushing)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/)
 * I have the following code on a custom page template to retreive particular posts.
   I can’t seem to figure out what to add to this so that I can also get posts of
   a particular category. Any help would be appreciated. Thanks.
 *     ```
       <?php
        $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
           WHERE wposts.ID = wpostmeta.post_id
           AND wposts.post_status = 'publish'
           AND wposts.post_type = 'post'
           AND wpostmeta.meta_key = 'sidedate'
           AND wpostmeta.meta_value > NOW()
           ORDER BY wpostmeta.meta_value desc
        ";
        $pageposts = $wpdb->get_results($querystr, OBJECT);
       ?>
       <?php if ($pageposts): ?>
         <?php foreach ($pageposts as $post): ?>
           <?php setup_postdata($post_title); ?>
           <li><a href="<?php the_permalink(); ?>"><?php the_title();
       ?></a></li>
        <?php endforeach; ?>
       <?php endif; ?>
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/#post-1724127)
 * There’s some examples in [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Category](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Category)
 *  Thread Starter [tcushing](https://wordpress.org/support/users/tcushing/)
 * (@tcushing)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/#post-1724361)
 * Awesome, thanks!
 * One more question – can you please direct me to where I can find out how to set
   the ORDER BY to a post tag for the code below instead of the meta value:
 *     ```
       <?php
   
        $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
           WHERE wposts.ID = wpostmeta.post_id
           AND wposts.post_status = 'publish'
           AND wposts.post_type = 'post'
           AND wpostmeta.meta_key = 'mathdate'
           AND wpostmeta.meta_value > NOW()
           ORDER BY wpostmeta.meta_value desc
        ";
        $pageposts = $wpdb->get_results($querystr, OBJECT);
       ?>
       <?php if ($pageposts): ?>
         <?php foreach ($pageposts as $post): ?>
           <?php setup_postdata($post_title); ?>
           <li><a href="<?php the_permalink(); ?>"><?php the_title();
       ?></a></li>
        <?php endforeach; ?>
       <?php endif; ?>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/#post-1724371)
 * You’d have to add JOINs for the taxonomy table(s) then do a ORDERBY one of those
   fields.
 *  Thread Starter [tcushing](https://wordpress.org/support/users/tcushing/)
 * (@tcushing)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/#post-1724417)
 * Thanks! I’ll chip away at it!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Get categories’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [tcushing](https://wordpress.org/support/users/tcushing/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/get-categories/#post-1724417)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
