Recently I had a run in with coding with a client who had a developer put together a sloppy custom WordPress query in place of a proper loop. In fact, it was amazing because it was put in replace of code on a great premium theme by StudioPress. By the way, no it was not StudioPress who did it, so let me get that out of the way. They are good at what they do. 🙂
Anyhow, the custom query was three different pieces of code: a section to show to first 5 most recent posts, and then two columns underneath to show two different categories that the site owner wanted to feature. The was done using a similar solution to the one At Weblog Tools Collection called Define Your Own WordPress WP-Query.
The problem was that this solution was not providing a good pagination. The “Next” and “Previous” did not work, and even if I wanted to use my favorite WP-PageNavi plugin, then I was pretty much – “sorry, out of luck.” This does not work well as a replacement for the regular loop, especially for what it was being used as.
So, I set out to put together a better solution. In the end, I displayed the most recent posts by normal Loop means. For the categories, I used the following:
<?php $recent = new WP_Query(); ?> <?php $recent->query('cat=1&showposts=3'); ?> <?php while($recent->have_posts()) : $recent->the_post(); ?> <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></h3> <div class="attachment-post-thumbnail"> <?php the_post_thumbnail(); ?> </div><?php the_content_limit(130, ""); ?> <?php endwhile; ?>
We are still using a custom WP_Query, but only using it for the categories. You can replace the 1 after cat=1 to whatever category you want to display, and you can change the number of posts displayed by changing the number in showposts=3.
You can customize the code further with CSS and adding other attributes.
I left some styling to include the thumbnail, title, and a short excerpt, even using the_content. However, you can change it to the_excerpt if you have set your excerpts through your functions.php or excerpt plugin. This code goes in your WordPress theme template where you want it, however I use this normally for the theme’s template file that is for the front page of the website. This is good for sites that are more magazine-like.
Sound easy?
Henry says
Can you do something similar for custom post types? I would like to be able to display a list of posts (maybe 5 posts) that are of the CPT “events” but am struggling with the code.
Chris says
I tried this and could not get it to work. I’m probably doing something wrong, but then i found out my theme has a widget that can display posts from a certain category in the sidebar which I’ve since started using instead. I’m sure it’s something with my theme.
Katie Woodard says
It sounds like your client had gotten ripped off. Good thing they had you to turn to. This looks a little complicated. I wouldn’t have been able to help them.
Ricardo says
Hi Nile,
I have come across both “WP Query” and “query_posts” and seen both approaches used for the same thing.
Can I ask what the difference is? I am trying to get my head around the WordPress loop.
Thanks!
Jonathon says
Found this interesting although coding is outside my skill set. I had used Flexibility3 free theme on 5 of my sites because I had seen other sites using it that I liked the look of. Being a non-techie I found trying to configure the sites frustrating so on the advice of a more experienced marketer I’m looking at the Studiopress premium themes which I understand are reasonably easy for the technically challenged to set up. Would you agree?
Aaron Turner says
Customized wordpress queries are the way to go, people want what they want and how they like it and the more unique things are even better.
Mel says
I have been searching for such a long long long time for a wp_query that worked. Thank you so much. You really made my night. Definitely subscribing to you. My site is almost ready but I am still figuring out how to display the different categories on the index page. =/
Anonymous says
hi i am looking for custom_type can you tell me for this i have suscribe your site to look forward thank u
Nile says
If you have custom post types and have enabled categories, you can put the Loop for the custom post type in the wp query instead.
jobin says
Thank you for a post….This looks a little complicated.
http://www.findghar.com