Creating a Page Loop with a Custom Select Query

I hit a roadblock today in one of my projects, it requires a page template that would display a list of pages. Listing posts based on a category is basic with query_posts but I needed to do the same thing for pages. I ended up posting a tweet and Dean responded shortly after with a link to the WordPress codex explaining the custom select query (I love the -web-).

After reading though the doc I was able to create this:

<?php
$querystr = ”
    SELECT wposts.*
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id
    AND wpostmeta.meta_key = ‘repertory’
    AND wposts.post_type = ‘page’
    ORDER BY wpostmeta.meta_value DESC
 ”;
 $pageposts = $wpdb->get_results($querystr, OBJECT);
?>
 <?php if ($pageposts): ?>
  <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

I’ll break it down just a little, you can read the doc if you’d like to learn more.

    

AND wpostmeta.meta_key = 'page-category' 

“page-category” is the key that I’ve assigned to the page. This is creating a category for your pages. I don’t recommend creating these false categories if unnecessary, use tags, I’m using these because I need to sort the pages based on the value.

    

AND wposts.post_type = 'page' 

Basically, only display pages.

    

ORDER BY wpostmeta.meta_value DESC

Finally, sort the pages based on the key value.

About the Author, Dan Cameron:

I'm the owner and solution engineer at Sprout Venture, a web solutions company that specializes in web development including WordPress.

I started my first blog in 2003 and transitioned to WordPress in 2004. Since moving to WordPress I've written a few plugins and themes for public consumption. Lately I'm busy engineering/building/coding and have only been able to share a few code snippets.

If you're in need of some web development, web design or custom WordPress plugins and/or themes contact me, I'll be happy to discuss it with you.

Read More »

  • at133
    Does anyone know how you would write a filter that would sort the posts using the values of two custom fields? I can't find anything about filtering by metavalues like JaredB mentioned. Also, is it possible to cast meta values in query posts?
  • Jord
    I also discovered this today, tis quality. Was able to sort a list of posts according to a decimal value in a custom field. Did try query_posts for a bit, but found that orderby was quite limited in what it could do. I found the above solution to fit the bill quite nicely.
  • You may have already tried this, but...

    I noticed you mentioned query_posts, but was the only issue with using that the fact that you wanted pages not posts?

    If so, you can specify the post_type as an argument to query_posts, like so:

    query_posts(array('post_type'=>'page'));

    Of course you could add any other arguments you may want. If there's any way at all you can go with query_posts, that's definitely recommended, since it is less likely to break on future changes than a direct SQL query is.
  • At first I was going to use query_posts but I needed to filter on meta fields and sort on keys. I'm not sure if query_posts facilitates meta - especially the sort.
  • I believe it does support both filtering by meta key/value and sorting by meta value (not key, though). I'm not positive that it actually works, but from looking at the code it looks like it's supposed to work at least.

    If these didn't work for some reason, you could also hook the filters (in your theme's functions.php) and make some final tweaks if needed to the query it generates, rather than doing the whole thing from scratch.

    Of course, if this is working for you now, you may not want to bother going back and changing it. I think it's just a tradeoff for a bit more effort now to mitigate the increased risk of breakage in the future from upgrades.
blog comments powered by Disqus