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.
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?
Hmm. Do you have an if statement wrapping the code?
Sos un groso! Hacia mucho que venia buscando esto y no lo encontraba.
Funciona de 10, muchas gracias!
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???
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_queryshould 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:Replace Cat_ID with the category ID.
Useful information there
Elegant solution to a common problem. I like it.
I always stumble upon your blog whenever I am searching for a WordPress coding solution.
Thanks!
thanks man! that came through in the clutch, was simple to follow, and worked! woot!
Ya it’s good but the_content()s’ more option doesn’t work ,
Please give any solution
Obrigado!!
Thank you just what i need!!
Thanks a million…