How To Create Custom WordPress Queries
Ok so except answering comments, I have been on a little bit of a hiatus, but now I am back. One thing that has been bugging me is that a few people are complaining of The Attached Image not working in WP 2.8. I can’t find any problems & I have asked a few people I know that are using the plugin on their clients websites, and they aren’t having any problems either, not even with the upgrade to 2.8.1. So until anyone can offer up a specific error or problem I can’t do anything, since it works fine for me.
Anyway. One of the most common questions I get is why The Attached Image won’t work with a custom query. Generally the query will look like this:
1 2 3 4 |
$my_query = new WP_Query('showposts=4&cat=2'); while($my_query->have_posts) : $my_query->the_post(); //post code endwhile; |
Well that shouldn’t work, I’ve seen it work in some rare instances, but it shouldn’t, for the The Attached Image I mean. The main reason is because to get the plugin to work correctly I have had to limit it to reading queries straight from $wp_query
, this is because of lots of problems with the $post
& $posts
variables.
There is an extremely simple way to fix this anyway. From my experience it also saves memory, which is very precious on a server, and seems to run quicker. This is also the way I was tought to make custom queries. I am working on making the plugin work with the other type of query, for compatibilities sake, but for the minute I haven’t been able to.
Anyway here is how, in my opinion, you should write them:
1 2 3 4 5 |
$tmp = $wp_query; $wp_query = new WP_Query('showposts=4&cat=2'); while(have_posts) : the_post(); //post code endwhile; $wp_query = $tmp; |
The reason I think you should? Well if you need, say 4, custom queries in your template you would have 4 full query variables with everything stored in them using the old way, wasting what could be, a lot of resources & memory. If you do it this way you only ever have 2 queries, the original (stored in $tmp
) & the new one, no matter how many custom queries you make.
Again I will keep looking for a way to make the other custom queries work, but for the minute this is the best I can think of. Who knows, according to 2.9 develoment notes they might be integrating the ability to show thumbnails per post, so The Attached Image might become redundant. 🙁
Anyway I hope that sheds light on why custom queries sometimes don’t work with The Attached Image, and shows what I consider to be a more efficient way to create a custom query.
2 Comments
Damian
I am new to use of word press, I have seen that you page look great. I want to let users to query my tables how can I do that? My page is about soccer statistics. I have learned how to show table son wordpress but I want something more like buttoms for some queries or ajaxs spry downs. Thanks
Paul Robinson
It is possible to manually query tables in WordPress however it isn’t something I’ve done before, so unfortunately I’m unable to help with that.
Also personally I would advise against it without carefully learning the sanitization functions available in WordPress and using them as much as possible.