Use SQL-querys in the loop – WITH template tags

2010-01-16 @ 14:35

The problem

I want to use SQL-querys within the WordPress loop and still be able to use the template tags. In this example I want both posts and pages within the loop. How can I do that? In both query_posts and WP Query I can only get specific posts OR pages, not both.

I’ve been searching the web for a way to combine multiple WP Query objects somehow, because I only want one loop.

The solution

The solution is by not using WP Query at all. This is how it works:

  1. Write a SQL Query. This gives you a great freedom to just select the post / pages you need, or a combination of them.
  2. Create a result object by using the function get_results.
  3. If I get a result, loop it out in a foreach loop.
  4. Use the function setup_postdata to be able to use WordPress template tags.
$query_sql = "
     SELECT *
     FROM $wpdb->posts
     WHERE post_type = 'post'
     OR post_type='page'
     ORDER BY post_title DESC

$query_result = $wpdb->get_results($query_sql, OBJECT);

<?php if ($query_result): ?>
     <?php foreach ($query_result as $post): ?>
          <?php setup_postdata($post); ?>
          <h2><?php the_title(); ?></h2>
          <?php the_content(); ?>
     <?php endforeach; ?>
<?php else : ?>
     <p>Not found</p>
<?php endif; ?>
RSS-feed for comments

7 replys to “Use SQL-querys in the loop – WITH template tags”

  • Moa »
    2009-12-21 @ 2:46 e m

    Found this while looking for a way to do exactly what you defined as the problem, having both posts and pages in one loop. I really wanted to find a way to combine two different queries into one result set, but this works almost as well – thanks a lot!

  • BrenFM »
    2010-11-08 @ 3:48 f m

    REferring back to your original post on Stack Overflow… couldn’t you use the following:

    $my_query = new WP_Query(array(‘post_parent’ => 3, ‘post_type’ => array(‘post’,’page’)));

    usually works for me…

  • outsource design »
    2010-11-27 @ 10:41 f m

    While we are talking about Use SQL-querys in the loop – WITH template tags – Multiple WP query objects / queryposts combined |, Another great option is to use a Flash intro template. If you have Flash, this is the perfect way to get a high quality Flash intro, without having to hire a Flash expert.

  • Oliver
    2011-06-01 @ 4:29 f m

    I had a different problem, but your solution to your problem helped me a lot. Thank you.

  • Phoenix
    2012-05-14 @ 6:08 f m

    Thanks Jens. After mucking about with so many WP_Query sites online, I just tried your very simple and powerful code. So tweakable!

    Question: do you know the ”taxonomy” type for comments and BBPress posts? I want a front page that includes posts, pages, comments, and BBPress pages all in the same query.


  • Phoenix
    2012-05-14 @ 6:11 f m

    Btw, this blog does not have a ”Subscribe to comments” function, so if you respond, could you pls write to me. Thx!

  • Andrej
    2013-01-12 @ 1:18 e m

    Thank you for your post. This could be the right solution of my problem. BUT: what i shall do for this query: i want to show posts from category A AND pages in the same query? Thank you for your help.

Leave a reply