Title: Displaying Posts Using a Custom Select Query
Last modified: August 18, 2016

---

# Displaying Posts Using a Custom Select Query

 *  [smoczokwik](https://wordpress.org/support/users/smoczokwik/)
 * (@smoczokwik)
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/)
 * In a struggle to have the posts on my category pages listed by custom field I
   tried the following code (based on [Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)).
 * The problems:
    1. It sorts the entries by date anyway 🙁 2. It does not respect
   the chosen category (i.e. dispalys all posts)
 * The second time I approach this problem and again it outwits me. **Help please!**
 * [moderated: code pasted at [http://wordpress.pastebin.ca/548100](http://wordpress.pastebin.ca/548100)]
 * To see the script in action go to:
    [http://www.sobek.pl/category/metoda-callana/stage-4/](http://www.sobek.pl/category/metoda-callana/stage-4/)

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576496)
 * It would seem your ORDER BY is not necessary since you are only selecting posts
   that have one specific meta_value (paragraf).
 * Instead of putting that code in a category template, try putting it in a [page](http://codex.wordpress.org/Pages)
   template.
 * Also see:
    [Template Hierarchy](http://codex.wordpress.org/Template_Hierarchy)
   [Stepping into Templates](http://codex.wordpress.org/Stepping_into_Templates)
 *  Thread Starter [smoczokwik](https://wordpress.org/support/users/smoczokwik/)
 * (@smoczokwik)
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576501)
 * Thanks, but actually I’m trying to get all the posts that have the meta key ‘
   paragraf’ set and sort them according the the ‘paragraf’ value. In other words,
   the meta key is needed to limit the posts I want to show, the value is used to
   sort these posts.
 * Indeed, the error was here:
    **NOT** ORDER BY wpostmeta.meta_key ASC **but** 
   ORDER BY **wpostmeta.meta_value** ASC
 * There is still the second problem left:
    How do I make it show only entries of
   the chosen category? it works with the standard tags, so I guess there must be
   a variable to pass along to achieve the script only showing posts of one category.
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576504)
 * Did you try to use it in a [category template](http://codex.wordpress.org/Category_Templates)?
   
   Say, your category has ID# 5, use a template file: category-5.php
 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576523)
 * If you want to do it by category as well, you need to restructure your query 
   a bit… That example they use is overly simple anyway.
 *     ```
       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 = 'paragraf'
       AND $wpdb->posts.post_status = 'publish'
       AND $wpdb->posts.post_type = 'post'
       AND $wpdb->post2cat.category_id IN (1,2,3)
       ORDER BY $wpdb->postmeta.meta_value ASC
       ```
   
 * Change the 1,2,3 with your category ID numbers, of course.
 *  Thread Starter [smoczokwik](https://wordpress.org/support/users/smoczokwik/)
 * (@smoczokwik)
 * [18 years, 12 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576542)
 * [@moshu](https://wordpress.org/support/users/moshu/) & Otto42
 * Hmm, looks like your ideas would make a good combo :-). Since I need to e.g display
   only the posts of category 4 on one page, and category 5 on another the only 
   way out for seems to be making 12 pages (because there are 12 categories) and
   modify the query accordingly for each page.
 *  [kernowkernow](https://wordpress.org/support/users/kernowkernow/)
 * (@kernowkernow)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576961)
 * with wordpress 2.3 you need to update the sql query shown above to this:
 *     ```
       SELECT * FROM $wpdb->posts
       LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
       LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       WHERE $wpdb->term_taxonomy.term_id = 1,2,3
       AND $wpdb->term_taxonomy.taxonomy = 'category'
       AND $wpdb->posts.post_status = 'publish'
       AND $wpdb->postmeta.meta_key = 'paragraf'
       ORDER BY $wpdb->postmeta.meta_value ASC
       ```
   
 *  [Modesty](https://wordpress.org/support/users/modesty/)
 * (@modesty)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576965)
 * Dear all, I’ve been struglling with this for a while now… I have a category that
   displays articles with basic info on some festivals… in my custom fields I have
   3 fields defining the date each festival begins… now I would like to sort all
   the articles in the category using these custom values… could you you help me
   to do that?
 *  [alanft](https://wordpress.org/support/users/alanft/)
 * (@alanft)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576966)
 * the easiest way might be to use add_filter( ‘posts_orderby’, X). try adapting
   the code used by Random Posts query code here: [http://themeshaper.com/wordpress-cms-plugins-the-bare-minimum/](http://themeshaper.com/wordpress-cms-plugins-the-bare-minimum/)
 *  [jan-londn](https://wordpress.org/support/users/jan-londn/)
 * (@jan-londn)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576968)
 * It’s working great here but i would like to limit the posts to lets say 10 posts,
   would that be possible? Thanks.
 *  [ninjaboy81](https://wordpress.org/support/users/ninjaboy81/)
 * (@ninjaboy81)
 * [18 years, 2 months ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576969)
 * I made a new discussion for my question: [http://wordpress.org/support/topic/161154?replies=1#post-707079](http://wordpress.org/support/topic/161154?replies=1#post-707079)
 *  [Ilir](https://wordpress.org/support/users/ipruthi/)
 * (@ipruthi)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576973)
 * Does this respect the “Blog pages per page” number specified in the WordPress
   admin?
 *  [Josh Byers](https://wordpress.org/support/users/jayberz/)
 * (@jayberz)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576977)
 * I would also like to know how to limit the query to a certain number of posts–
   just like ‘showposts’ does in the normal query.
 * Thanks.
 *  [dexno](https://wordpress.org/support/users/dexno/)
 * (@dexno)
 * [18 years ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576981)
 * i would like to know how to apply wp_pagenavi for this combo?
 * Thanks
 *  [valeeum](https://wordpress.org/support/users/valeeum/)
 * (@valeeum)
 * [18 years ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576983)
 * I can’t figure out how to embed this code within functions.php and get it to 
   work. Are there specific scope issues that need to be addressed?? When I copy
   the code above directly into category.php, it works fine. Thanks for the support!

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

The topic ‘Displaying Posts Using a Custom Select Query’ is closed to new replies.

## Tags

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

 * 14 replies
 * 13 participants
 * Last reply from: [valeeum](https://wordpress.org/support/users/valeeum/)
 * Last activity: [18 years ago](https://wordpress.org/support/topic/displaying-posts-using-a-custom-select-query/#post-576983)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
