Title: Custom Query Problems
Last modified: August 19, 2016

---

# Custom Query Problems

 *  Resolved [cmoloney](https://wordpress.org/support/users/cmoloney/)
 * (@cmoloney)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/custom-query-problems-1/)
 * Hello,
 * I think I posted this in the wrong section yesterday so I am reposting it.
 * I am having a problem creating a custom query on my site. Essentially I would
   like to achieve the same query listed as one of the examples on this page on 
   wordpress.org:
    [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 * I want to find the posts in a particular categories and then sort the results
   by custom field value. The website I am designing this for is [http://www.theargentimes.com/agenda/](http://www.theargentimes.com/agenda/).
   The code in question is not up there because it returns no results.
 * There is an Agenda (category 329) and there are 9 subcategories (987-995). On
   all of these category pages I want to display the entries in ascending order 
   according to a custom field called ‘agendadate’. Here is the code i used. I am
   not sure what is wrong because it is exactly the same as the code in the example.
   The result is that no results are returned. There are definitely posts in this
   category with the appropriate custom key.
 * I use custom queries on other parts of the site but I can never get it to work
   with joins. Hope you can help. Thanks.
 *     ```
       <?php  // Cinema and Film module cat 987  
   
        $querystr = "
           SELECT $wpdb->posts.*
           FROM $wpdb->posts
           LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
           LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id)
           WHERE $wpdb->postmeta.meta_key = 'agendadate'
           AND $wpdb->posts.post_status = 'publish'
           AND $wpdb->posts.post_type = 'post'
           AND $wpdb->post2cat.category_id IN (987)
           ORDER BY $wpdb->postmeta.meta_value ASC
           ";
   
        $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
   
        <?php if ($pageposts): ?>
         <?php foreach ($pageposts as $post): ?>
           <?php setup_postdata($post); ?>
            <?php edit_post_link('Edit', '<small>', '</small><br />'); ?>
            <?php the_content(); ?>
          <?php endforeach; ?>
         <?php else : ?>
         <p>DOH!</p>
       <?php endif; ?>
       ```
   

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

 *  [iridiax](https://wordpress.org/support/users/iridiax/)
 * (@iridiax)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/custom-query-problems-1/#post-973655)
 * You used the pre-Wordpress 2.3 query. Instead, use the one immediately following
   it.
 * [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 *  Thread Starter [cmoloney](https://wordpress.org/support/users/cmoloney/)
 * (@cmoloney)
 * [17 years, 3 months ago](https://wordpress.org/support/topic/custom-query-problems-1/#post-973665)
 * iridiax – problem solved – thanks!
 * Man that was a simple mistake. I just glanced at the examples thinking that was
   for pre 2.3 and have racking my brain over the code since then.
 * You saved me a lot of time. Thanks again.

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

The topic ‘Custom Query Problems’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [cmoloney](https://wordpress.org/support/users/cmoloney/)
 * Last activity: [17 years, 3 months ago](https://wordpress.org/support/topic/custom-query-problems-1/#post-973665)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
