How To Create Custom WordPress Queries

/ Wordpress / by Paul Robinson / 2 Comments
This post was published back on July 14, 2009 and may be outdated. Please use caution when following older tutorials or using older code. After reading be sure to check for newer procedures or updates to code.

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:

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:

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.


Author’s gravatar

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

Author’s gravatar author

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.

Older Comments
Newer Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

I'll keep your WordPress site up-to-date and working to its best.

Find out more