Title: Querystring for pages (not posts)
Last modified: August 19, 2016

---

# Querystring for pages (not posts)

 *  [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/)
 * Hi to all, i hope that someone can help me to understand better the querystring
   for pages.
 * I’ve succesfully used the querystring code showed [here in the codex](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query):
 *     ```
       $querystr = "
       	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)
       	LEFT JOIN $wpdb->terms ON($wpdb->terms.term_id = $wpdb->term_taxonomy.term_id)
       	WHERE $wpdb->terms.name = 'slides'
       	AND $wpdb->term_taxonomy.taxonomy = 'category'
       	AND $wpdb->posts.post_status = 'publish'
       	AND $wpdb->posts.post_type = 'post'
       	AND $wpdb->postmeta.meta_key = 'order'
       	ORDER BY $wpdb->postmeta.meta_value ASC
       ";
       ```
   
 * Ok, that’s for query the posts, but what i would be glad to know is how to obtain
   the result with a similar code not for posts but for pages.
    The following example,
   got always from the codex page, is working, but not extensively as the previous:
 *     ```
       $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
           WHERE wposts.ID = wpostmeta.post_id
           AND wpostmeta.meta_key = 'custom-key'
           AND wposts.post_type = 'page'
           ORDER BY wpostmeta.meta_value DESC
           ";
       ```
   
 * Can someone help me?
    Thank you!

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/#post-1794022)
 * [http://codex.wordpress.org/Function_Reference/query_posts](http://codex.wordpress.org/Function_Reference/query_posts)?
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/#post-1794071)
 * Thank you for the reply esmi!
    I’ve already checked that codex page, but i haven’t
   success trying with the “query_posts” function, probably because i’m using meta
   keys and values to retrieve pages and to order them via different methods.
 * I think that’s better if i write the code i’m using, the first to list a series
   of posts title&thumbnail on a page, the second one to list a series of pages 
   title&thumbnails on another page.
    The last one obviously haven’t the sort order
   working correctly as the first code example.
 * Code for displaying a series of posts title&thumbnail in a page with two links
   that produce a different sorting (full working):
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com)]_
 * (Sorry for the longer code and post)
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/#post-1794105)
 * Sorry, for the previous post, here are the pastebins.
 * The first code, to show posts list with title and thumbnails in a page, with 
   two succesfully working links for sorting the list is the following:
    [Code for posts (on pastebin)](http://wordpress.pastebin.com/A3crhTGb)
 * The second code, to show pages list with title and thumbnails in a page, with
   the two not working links for sorting the list is the following:
    [Code for pages (on pastebin)](http://wordpress.pastebin.com/DnwZMZ1G)
 * By the way today i have learned a bit more about SQL via the [w3schools website](http://www.w3schools.com/sql/default.asp),
   so i’ve tried to access to the wpdb with my way for the pages query:
 *     ```
       $querystr =
               "SELECT wposts.*
       	FROM
               $wpdb->posts wposts,
               $wpdb->postmeta wpostmeta,
               $wpdb->terms wterms
               WHERE wpostmeta.meta_value = 'Gaming'
       	AND wposts.post_type = 'page'
       	AND wterms.name = 'Gaming Design'
       	ORDER BY wpostmeta.meta_value ASC";
       	$pageposts = $wpdb->get_results($querystr, OBJECT);
       ```
   
 * If i don’t get wrong, this will get the pages from the wpdb where the meta_value
   is equal to Gaming and where the category is Gaming Design.
 * But what i’ve on the output are all the pages, including the pages that haven’t
   meta_value equal to Gaming and also the pages that have category not equal to
   Gaming Design.
 * Probably i wrote something wrong, but what?
 * Thank you for the help!
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/#post-1794130)
 * Today i’ve tried other ways but got no solutions so far.
    I’m stuck at the previous
   post.

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

The topic ‘Querystring for pages (not posts)’ is closed to new replies.

## Tags

 * [post order](https://wordpress.org/support/topic-tag/post-order/)
 * [Query String](https://wordpress.org/support/topic-tag/query-string/)
 * [querystr](https://wordpress.org/support/topic-tag/querystr/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [Rics1983](https://wordpress.org/support/users/rics1983/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/querystring-for-pages-not-posts/#post-1794130)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
