Jan
28
2009

Using WP_Query to Fetch Posts in WordPress


I was just putting together a WordPress widget for a client and I came across a small road block. I was using <?php query_post(); ?> to set the loop to act the way I needed it to but when the widget loaded up in the left sidebar, it caused some issues when going to a category page. The problem was the query I initialized in the sidebar was carrying over to the main section. I couldn’t figure out what to do until I stumbled up the wp_query function.

It works in a similar way to query_post but you need to do a few minor things to get it to work properly.

<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=3');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<div class="meta">
By <?php the_author() ?>
</div>
<div class="storycontent">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>

First you need to setup the query.

$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=3');

Then you need to setup the loop.

while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
     (stuff goes here)
<?php endwhile; ?>

So in the end it looks something like this.

<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=3');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<div class="meta">
By <?php the_author() ?>
</div>
<div class="storycontent">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>

What this will do is get 5 posts from category 3 and list them, without interfering with other loops or queries on your site.

If you liked this, please share it.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Short URL: http://bit.ly/d1v73p

Discussion 12 Comments

  1. Alex on June 15, 2009 at 3:45 am

    I tried the code above on a test website and it works..but when i tried on the real thing…it doesn’t. I mean on the home page nothing is displayed(title and post in sidebar) but when i access a post or category it shows what is should have shown. Any ideas why?

    • c.bavota on June 18, 2009 at 1:36 pm

      Hmm. Do you have an if statement wrapping the code?

  2. Facundo on August 19, 2009 at 1:17 am

    Sos un groso! Hacia mucho que venia buscando esto y no lo encontraba.
    Funciona de 10, muchas gracias!

  3. TechSlam on October 20, 2009 at 3:18 pm

    I have a question.
    Inmy WP theme, i have a home page with a sidebar.

    the home page displays latest 5 posts of a category named “Features” and on the same page the sidebar displays the latest 5 titles of another category named “news” and both these have their category name as heading. The problem is I am not able to get the proper category name to show up, it shows the “news” category name on both the sections. i am using query_posts(). I did not try wp_query, but after looking into your code, i feel that it will give me the same issue. I mean, can’t I use the_category before the while loop to fetch the respective category name???

    • c.bavota on October 21, 2009 at 10:52 am

      query_posts() should only be used once. That is the main loop that usually is where you a calling your main list of posts. wp_query should be used for every other other query. the_category() always has to be in the loop unless you are on a category page, then it can be outside. How is it suppose to know what category to call if you have not created a loop calling posts for that category. If you don’t want to have it within the loop, you can always use:

      <?php echo get_the_category(Cat_ID); ?>

      Replace Cat_ID with the category ID.

  4. Robert Stanson on November 14, 2009 at 7:23 pm

    Useful information there

  5. Danno on March 29, 2010 at 9:33 pm

    Elegant solution to a common problem. I like it.

  6. Ryan on April 20, 2010 at 3:21 am

    I always stumble upon your blog whenever I am searching for a WordPress coding solution. :) Thanks!

  7. andy on April 27, 2010 at 6:51 am

    thanks man! that came through in the clutch, was simple to follow, and worked! woot!

  8. Gokul on May 21, 2010 at 7:13 am

    Ya it’s good but the_content()s’ more option doesn’t work ,
    Please give any solution

  9. Israel Morales on August 5, 2010 at 10:09 am

    Obrigado!!

    Thank you just what i need!!

  10. Fin on August 22, 2010 at 10:52 am

    Thanks a million…

Leave a Reply

Your email address will not be published. Required fields are marked *

*


To enter code in the comment box, please place it between <pre lang="php"> </pre> tags. You don't have to convert any characters to their hex/HTML code. Just add your code the way you would to any code editor.