Well I was going to change to blog as I mentioned a while ago, but I’ve been very busy and haven’t had time yet. Keep a look out after Christmas though and things should start to change then. Until then though here is a little tutorial for all you WordPress users.
Using WordPress is relatively simple, [...]
Well I was going to change to blog as I mentioned a while ago, but I’ve been very busy and haven’t had time yet. Keep a look out after Christmas though and things should start to change then. Until then though here is a little tutorial for all you WordPress users.

This updated posts slider is a great example of what WP_Query can do.
query_posts(), which is fine, but if you want a featured post set, or a little updated posts section (picture on the right) you will want to use WP_Query().
WP_Query(); is used to create a new query that is independant of WordPress’ normal query, this means you can show the posts as normal & have another feature showing updated posts as I have shown you above. First let look at how to create a new instance of WP_Query().
$wp_query = new WP_Query();
The problem here is that WordPress’ normal query is also held inside the variable $wp_query. You would automatically think that using a different variable would work, well it won’t. If another loop is still held as active inside $wp_query it stops certain commands, such as the_tags() from working. So instead we do a little switching. Like this:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('');
You can use any attribute that you would use in query_posts(). You can find all about query_posts() at the WordPress Codex. You might know all this, but something that most people don’t know is that if you use a plugin that needs to use the $posts array it won’t be poplulated with the correct posts. It will actually be populated with the posts from the main WordPress loop. To solve this, you need to re-set the $posts array. You can do it like this:
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5');
$temp_posts = $posts;
$posts = null;
$posts = $wp_query->posts;
Then any plugins that need to use the $posts array will work fine.
That’s it. I hope that helps you with how to make custom queries in WordPress. Any problems then just drop me a comment. If you want to see an example of what custom queries can do then visit Purrfect Gifts @ Zazzle.

25 Responses
Hi,
Im trying to do exactly what you mentioned in the narrative in one of my custom plugins. I tried your code but Im getting this error:
Call to undefined function is_user_logged_in() in /Applications/xampp/xamppfiles/htdocs/wordpress/wp-includes/query.php on line 2127
Any tips on theses?
The error msg you are getting seems to indicate that you have a problem somewhere in WP’s main files since there is a function called
is_user_logged_in().I can only suggest downloading a fresh copy of WP and then deleting the entire wp-includes directory & then pasting in the new one. Don’t overwrite it tends to cause problems.
I’ll be happy to help more if that doesn’t help.
Thanks alot for you explanation Unus, trying your code now. Was working a few days on this, but didnt know it was that easy!
Thanks again!
Regards,
Aislin
I am using WP Query to generate posts on a static page. I only get a blank page. Maybe because the query is taken? See my WP Post here:
http://wordpress.org/support/topic/260984?replies=6
Refering to the code in your last post on the WP forum. You have cut off the if..else.
You start with the WP_Query & a while loop. Then if you loop just after the navigation section there is an else but you never started an if to match it. Since WP tries to hide Fatal errors (for security reasons) it sometimes shows a blank page instead, that’s probably what is happening.
Either remove the if..else completely (not adviseable) or add in the if and all should start working. Hope that helps you.
Hi.
I’m trying to use WP_Query to create a custom loop on the front page, but I can’t get it to work with pagination (next_posts_links). The first two pages are fine, but after that it will only display 404. The thing is, it only failed when using pretty link, but when I sitched back to default style (?p=XX) it works. So I think it has something to do with the $paged variable. Any clue will be appreciated. Thanks.
Hi there kucrut,
Well usually I’ve only ever seen these types of query used to make featured post areas (you know like the javascript slider things), so I’m not really sure what’s causing that.
It’s very bizzare (and interesting) that it works with pretty permalinks turned off, not sure what it means though unfortunately. I’m guessing that you’ve already tried appending the paged variable, if not it goes like this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ... $wp_query->query('showposts=5&paged='.$paged); ...That should work. If you have already tried that though, then I’m stumped, sorry.
If anyone else can help feel free to drop a comment.
Thanks for your quick response.
Yes, I forgot to mention that I have that $paged variable appended. I even tried to echo the value after the loop, and when I reached page three it shows nothing (with or without the $paged var appended).
Anyways, thanks again for the response.
PS. I’m having trouble to do some search here in this site, it keeps sending me back to the front page, is it just me?
Hi,
I’m officially all out of ideas then, sorry.
Nope not just you, I’d installed a new firewall plugin (the site has been hacked a lot recently) & I forgot to allow the search variable through the firewall. I’ve done it now though so searches should be fine. If they aren’t let me know & I’ll investigate further.
A strange question, but does normal blog navigation allow you past page three? By that I mean when viewing the blog as normal & not the custom query.
Yes, the default loop works fine.
The search is also working fine. Thanks.
I’m afraid I haven’t a clue.
Sorry I can’t be of more help. I might be a daft suggestion, but if you have a localhost try installing a new copy of wordpress & doing the same alterations on that install, unless that’s what you are doing of cause, just incase it has something to do with that install.
It’s defo a strange one though, I’ve neither heard of it or seen it before.
Sorry.
Now I can finally breath… it works!
.htaccess is the culprit. I new i deleted it many times and then generate the new ones (as suggested somewhere in wordpress codex), using random style of permalink but I couldn’t get to work. Few minutes ago (just before my head was going to explode) I thought I’d give it one more try… and it works!
Thanks again!
Ahhh. Nice work.
Thanks for letting me know, if anyone else has the problem I can help them now.
Hi there… well… I tried to add the “$posts variable suplantation” just in case any plugin might need it (I think “Ajaxed WP” does) but I got a “Fatal error: Cannot access empty property in (myfile)” so I tried changing the
in your example for:
… and I don’t get the error… so… are you sure it’s the other way? I haven’t tried the plugins yet.
Hey there Cosme. Your right, it shouldn’t have that $ there, I’ve no idea how I missed it, I’ve no idea how the code has been working for the other commenters either, because you’re right that $ will cause an error.
I’ll correct the post now. Thanks for pointing it out.
Yep… well… maybe they just obviated the issue, I’m glad to help. By the way, thanks for sharing the knowledge, great blog. This particular tweak is hardly found.
No problem. Your probably right. Again thanks for telling me about it, I probably wouldn’t have found it otherwise.
It I think it helpfull for me, but I needed your help as I creating a plugin which returns a complete mysql query according to our custom requirments for getting posts, comments, pages ect.
So 1st is that I’m required to display posts according to that generated query at home page.
and 2nd is, some time query has complex conditions then how I’m able to manage these kind of complex customized queries for getting required results.
I think I understand. However accessing the MySQL query that WordPress generates directly (if it’s even possible) can be dangerous. You can do pretty much anything you like using the options given in
WP_Query()but for those times where it still isn’t customiseable enough you will need to write a plugin that makes it’s own connection to the database using WordPress’ database functions. Unfortuntately I haven’t had any experience with that part of WordPress. So am unable to help you.Does anyone know why the search template is negating any new WP_Query instances I would create.
This includes the one in the “Recent Posts” widget.
For example add “Recent Posts” widget to a global sidebar. Show that sidebar on the search.php template. Then watch it disappear. Looks like some how WordPress accomodated for this some how, but in my sub queries I am not, the way they are.
Anyone have an idea about this.
To note: I have already tried the following…
$myQuery->query_vars['s'] = ”;
$myQuery->is_search = false;
// Start the Sub Loop…
Hi Jim,
I’m not sure. I thought it might have been a bug in WordPress, but I’ve just tested it & my recent posts widget works fine when searching.
As a rule of thumb I always run the search through WP main query system instead of making a separate one, meaning overwrite
$wp_querywith the new query, as I find it stops all sorts of strange things happening. However, I don’t know if that would solve your problem.Sorry if I haven’t been much help. It’s a rather stange problem.
Hi Veneficus and Paul,
Thanks for the exellent trick with WP_Query. I hope you can help me with basic problem.
I intend to make a custome query which will showcast only featured posts in my homepage. However, on using custom query, pagination doent seem to work.
The custom query is –
have_posts()) : $recent->the_post();?>
*** Do some stuff ***
Now, the problem is even after clicking “Older Entries” or “Newer Entries”, the page URL changes to domain.com/page/2 but the contents remain same.
It looks like pagination isnt working with WP_Query.
I would be really greateful if you can suggest something to make it work.
Normally with featured page items you don’t want them to change when you change pages, but it is easy (once you know how) to allow it to change pages.
Just do this with your custom query:
//put this above your custom query $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //now for the custom query example $wp_query = new WP_Query('cat=5&posts_per_page=5&paged='.$paged);Hopefully that will help.
P.S Sorry about the code highlighting not working. I’m having a few problems with the plugin I use. I’m trying to fix it now.
Thanks for the quick response.
Let me try this new way.