55 Comments
Found in Tutorials
June 24, 2010
Fetching Posts by Category in WordPress with jQuery (the Easy Way)
Using some simple jQuery and PHP, we’ll create a Recent Posts widget that you can filter by category without an annoying page refresh. No need to create extra WordPress pages and templates! We’ll also apply a little jQuery animation so your container height will glide smoothly into place.
NOTE: For a loose example of this tutorial in action, check out the sorter on my legacy portfolio.
1. Create Your Template
First, we need to create a file called querySort.php and place the following code inside of it:
<?php
// load wordpress into template. dont touch me!
$path = $_SERVER['DOCUMENT_ROOT'];
define('WP_USE_THEMES', false);
require($path .'/wp-load.php');
// ah, wordpress is loaded. balance has been restored.
query_posts($_GET["query"]);
?>
<div id="queryContainer">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<!-- place your post template here. -->
<?php endwhile; ?>
<?php endif; ?>
</div>
This is your entire template for the widget’s output. By including wp-load.php, we’re able to avoid the annoying need to declare the file as a WordPress template, create an empty Page, and apply said template to it. The most important part of this file is this line:
query_posts($_GET["query"]);
Upon each load, query_post will grab our query from a variable that jQuery passes to it. This variable contains, by default, options for the number of posts and a default starting category.
Style the loop as necessary. As long as the div “queryContainer” and the loop are maintained, you may style the output any way you’d like.
2. Sprinkle in Some jQuery
Create another file called querySort.js and place the following code inside of it:
jQuery(document).ready(function($) {
// replace these default settings with your own
$.querySort = {
path_to_template: 'http://www.YOURSITE.com/wp-content/themes/YOURTHEME/',
number_of_posts: 5,
default_category: ''
}
// do the initial load.
$("#querySort").load($.querySort.path_to_template + 'querySort.php' + '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + $.querySort.default_category);
//functions for the sorter
$("#queryMenu a").click(function(){
var thecat = $(this).attr("id");
if (thecat == 'reset') {
var query = '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + $.querySort.default_category;
} else {
var query = '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + thecat;
}
$("#querySort").animate({opacity: 0}, function() {
$("#querySort").load($.querySort.path_to_template + 'querySort.php' + query, function() {
height = $("#queryContainer").height() + 'px';
$("#querySort").animate({opacity: 1, height:height});
});
});
return false;
});
});
Let’s break this down to make it easier to understand. Here are the default options provided by querySort:
$.querySort = {
path_to_template: 'http://www.YOURSITE.com/wp-content/themes/YOURTHEME/',
number_of_posts: 5,
default_category: ''
}
You’ll need to add the absolute path to the template you’ll be using this with (dont forget the trailing forward slash). If you’ve installed WordPress in a subdirectory, make sure to update the url to reflect that. Number_of_posts controls precisely the number of visible posts at a time, and default_category is only really necessary if you wish to limit the visible posts to a specific category and its children.
// do the initial load.
$("#querySort").load($.querySort.path_to_template + 'querySort.php' + '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + $.querySort.default_category);
This loads the default, unsorted query as soon as the DOM is ready. Using the .load() function, jQuery loads querySort.php, appending a variable called query, which is then accessed by the wordpress function query_posts(). The contents of the div #querySort are replaced with the results. (if you customize this script in any way to add more filters, be sure your query is urlencoded.)
//functions for the sorter
$("#queryMenu a").click(function(){
var thecat = $(this).attr("id");
if (thecat == 'reset') {
var query = '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + $.querySort.default_category;
} else {
var query = '?query=showposts%3D' + $.querySort.number_of_posts + '%26category_name%3D' + thecat;
}
$("#querySort").animate({opacity: 0}, function() {
$("#querySort").load($.querySort.path_to_template + 'querySort.php' + query, function() {
height = $("#queryContainer").height() + 'px';
$("#querySort").animate({opacity: 1, height:height});
});
});
return false;
});
This is where the magic happens. This function accesses the div holding your sorting options, grabbing the ID of your link and replacing the query’s category name with its contents. Next, it fades out the #querySort div, loads the results of the new query, then fades them back in, fluidly adjusting the height of #querySort to match the new height of your content. This prevents ugly flickers or large gaps beneath the widget.
That’s all there is to it. One more step, and you should have a working widget!
3. Add the Widget to Your Theme
Now, assuming you know where you’re going to place the widget, you have but to place the following markup in your theme and apply your desired CSS styles.
<div id="queryMenu"> <ul> <li><a href="#" id="categoryslug">Category Name</a></li> <li><a href="#" id="reset">All Posts</a></li> </ul> </div> <div id="querySort"> <!-- leave me empty. --> </div>
The #queryMenu div is very simple to set up. Each anchor you create must have to category’s slug as the id. This will be used by jQuery to execute the filter. IMPORTANT: To reset the sorter, create an anchor with ‘reset’ as the id. If, for some unimaginable reason, you have a category called Reset, you may want to change this to something else in your jQuery.
4. Enqueue and enjoy.
Upload your new files to your theme’s directory. Enqueue querySort.js and pat yourself on the back. You should now have a working jQuery sorter!

Mark Fasel said:
Exactly what I was looking for! Great tutorial!
Reply to comment
admin said:
Glad it was helpful! Please let me know if you have any issues. I threw this tutorial together late at night and I’m sure it could be improved upon.
Reply to comment
David Wood said:
This is exactly what I’m looking to do! Unfortunately I’m trying to alter a theme and I’m a bit of an amateur, having trouble implementing it. I’ve put the querySort.php into my theme folder and linked to it in the js file… as well as linking to the js file of course but it’s not working? HELP!
Reply to comment
admin said:
Hello David, if you have a link for me to check out I can try to troubleshoot it for you. Most of the time its a missing or misplaced div here or there! You can email me the link directly if you’d prefer and I’ll get back to you with an answer.
Reply to comment
David Wood said:
Could you link me to the source files? I have emailed you but I doubt you have the time to trawl through code!
Reply to comment
Tim Mon said:
Nice Job! Thanks for sharing this! Gorgeous site too… I like it.
Reply to comment
Caroline Keim said:
Great Tutorial!
However, I’m getting an error –
Fatal error: Call to undefined function query_posts() in querySort.php on line 7
Any idea what could be wrong?
Reply to comment
taylor said:
Did you solve this error? I am getting it too.
Reply to comment
SK said:
Thanks for writing this up. I have some questions, I hope you don’t mind. The procedure described in part 1 appears as though it can be used to generate stripped down templates on the fly for the specific purpose of ajax .load or .get.
I’m currently building a WP site and I have a load more button at the bottom. With jQuery .get, I’m fetching the /page/2/ of a section for instance and appending it to the current page.
It works well, but I feel it could be faster since it currently fetches and has to parse through all the unnecessary parts of the subsequent pages.
Will using your code be applicable for things like the Load More button to retrieve a second page of results on the same home page? And if so, is it efficient? Even though my method is slow on the user-end, it is efficient for the server since WP can cache page/2/ page/3/ etc. However, since your code doesn’t seem to be part of a WP template, I’m afraid that important plugins like WP Super Cache and W3 Total Cache won’t work for high traffic blogs.
Reply to comment
Chad said:
Thanks for this. It is exactly what I’ve been looking for. My only problem is that I can’t get the other jquery script I was using to work inside the results container. Any suggestions?
Reply to comment
Jack Fearing said:
Hello, is there anyway to make pull the template path dynamically (using something like ) rather than the absolute path?
Reply to comment
Andrew Martin said:
Just wanted to say thanks a lot for the tutorial. This is my first time diving into Ajax and it was awesome.
I’ve modified a few things and taken this as a foundation; one thing I might suggest is to do it like this:
$.querySort = {
path_to_template: ”,
post_type: ‘review’,
number_of_posts: 5,
default_category: ”,
meta_key: ‘feature_flag’,
this_page: ‘post_name; ?>’
}
I only say this because I found it awesome to use that “this_page” variable to do some customization on the way the query uses the WP loop and thought this was a bit cleaner.
THANK YOU for posting this awesome tutorial.
Reply to comment
abid said:
good word
Reply to comment
Remi said:
I cant get this to work, you say: “Each anchor you create must have to category’s slug as the id. ”
But where do i put the id in:
Category Name
And in the beginning, where you say:
Can i put in the place of or does that have to be another loop?
Reply to comment
Renu said:
Thank you for this handy widget! I got it working now.
Maybe for the beginners its handy to mention that you have to add the line <script src="/querySort.js” type=”text/javascript”> to your header.
Anyway thanks
Reply to comment
Renu said:
Does anyone know how to exclude a certain categorie from the results the list displays?
I’m trying:
number_of_posts: -1,
cat: -4,
default_category: ‘werkzoekende’
}
// do the initial load.
$(“#querySort”).load($.querySort.path_to_template + ‘querySort.php’ + ‘?query=showposts%3D’ + $.querySort.number_of_posts + ‘%26category_name%3D’ + $.querySort.default_category + ‘%26cat%3D’ + $.querySort.cat );
but that doesnt seem to work
Reply to comment
marmont said:
C7mSBR http://sjI1mfH9Cx4hsDePoai2v.com
Reply to comment
Pharmb433 said:
Hello! aggcaee interesting aggcaee site! I’m really like it! Very, very aggcaee good!
Reply to comment
Pharmg109 said:
Very nice site!
Reply to comment
Pharmf627 said:
Hello! eecgeke interesting eecgeke site! I’m really like it! Very, very eecgeke good!
Reply to comment
Pharma836 said:
Very nice site!
Reply to comment
Yoshimi Rider said:
Amazing work!!! I’m working on a Commercial WordPress theme and I was wondering if it would be okay to use your code
Reply to comment
test said:
test
Reply to comment
Urson0Xk said:
QWpP1i http://www.2KFk8UxzgR3t2CjpiGYlWRZr9NzJwIs8.com
Reply to comment
Roshan said:
awesomeness
Reply to comment
Zidnyytl said:
Other amount http://ucekibireco.blog.free.fr/ porno japanise preteen dumb storyline… this is the same guy that did the storyline about fucking his grandmother… btw, germans donôt really think like that…
Reply to comment
Bkvnuhiq said:
How much is a Second Class stamp? http://pytilemugaa.blog.free.fr/ younger preteens naked fuck yeah,horny and entertaining,check out the guys smile when he twigged what was happening.if i was him i would have watched her playing with herself for a bit longer…
Reply to comment
Umomvctt said:
Very funny pictures http://jijoamycum.blog.free.fr/ naked preteen top harry..that is really hot. i have never done anything like that but have fantasized about it often. i would love to watch you eat the cum out of my wifes pussy or even my load out of your wifes pussy. your friends sound like the have the right idea. i could even cum in your ass.
Reply to comment
Ebgjfmtl said:
When can you start? http://tugydibejy.de.tl lol bbs guestbook gallery Another thing why the fuck is his dick limp man i would be fuckin rock solid just looking at her bro……..
Reply to comment
Wpuekpyq said:
Could I borrow your phone, please? http://hueisebej.de.tl nadia litle model Know that scene from Tomcats where McGuire comes out with three cups full of cum… yeah, something like that *faints*
Reply to comment
Spczchzu said:
Could you please repeat that? http://qygiikilily.de.tl ls models elwebbs Justin slayer always gets the ripe ones yo, for REAL. he fuckin those real rare hotties. this girl is like a thick juicy freak yo. ass and legs like NASA space rubber lol that shit DEFIES gravity. i love it.
Reply to comment
Ttokcore said:
In tens, please (ten pound notes) http://asomokilababy.de.tl early teen bbs Rachel starr is fuckin hot but there are a bunch of fags instead of watchin him hit tht they should be tryin to get pussy haha dumb fucks
Reply to comment
Aeyngtlx said:
Could you transfer $1000 from my current account to my deposit account? http://oruiecon.de.tl nymphettes sex i was over at my mates house and after a sick party i passed out in his moms bed i woke up and started to jerk to some porn his mom came in and looked at me and the next thing i knew i was getting a bj and we fukced for the rest of the night needless to say i was happy
Reply to comment
Meoffndo said:
What sort of music do you listen to? http://ygaesoly.de.tl boys bbs toplist porn Stunning from modelling to porn star. She’s got some drumsticks to work on with her credentials! Plus those pink boots really suited her…
Reply to comment
Dinusvvj said:
I’d like , please http://doraoqucyk.de.tl japanese lesbian models OH FUCK…. what a fucking playful sexy slut….man this girl gives me an INSTANT hard on…. I love her perfect tits and her perky little nipples…. her nice big fucking ass…. gorgeous eyes…. damn I’d love to have this girl as a nicely wrapped present for one night lol now I only wish the sound would be synced…. but oh well
Reply to comment
Axrvrajw said:
What are the hours of work? http://uriyriylo.de.tl model teenz Awesome vid. The guy really didn’t blow his opporotunity to have sex with a beutiful teen female and I fully respect that. Props to the ho AND the guy.
Reply to comment
Pharmk307 said:
Hello! ckedgbb interesting ckedgbb site! I’m really like it! Very, very ckedgbb good!
Reply to comment
Pharma132 said:
Very nice site!
Reply to comment
Pharmc301 said:
Hello! gfaefka interesting gfaefka site! I’m really like it! Very, very gfaefka good!
Reply to comment
Pharmk115 said:
Hello! kegdfde interesting kegdfde site! I’m really like it! Very, very kegdfde good!
Reply to comment
Pharmd47 said:
Very nice site!
Reply to comment
Pharme501 said:
Hello! eeeekgg interesting eeeekgg site! I’m really like it! Very, very eeeekgg good!
Reply to comment
Pharme450 said:
Hello! agkbgde interesting agkbgde site! I’m really like it! Very, very agkbgde good!
Reply to comment
Pharmk92 said:
Hello! gbedacg interesting gbedacg site! I’m really like it! Very, very gbedacg good!
Reply to comment
Klara said:
I think that this is exactly what I need!
For the site that I’m making Im filtering with isotope.js all my blogposts.
My issue is at the first appearence I want only 5 posts from category blogg (id 89) and when I filter on ‘blogg’ all posts will appear. I try to achieve this by merging the arrays.
pseudo-code
_____________
if(‘blogg)
$args_blog = array( ‘cat’ => ’89′ , ‘numberposts’ => -1 , ‘orderby’ => ‘ASC’);
else
$args_blog = array( ‘cat’ => ’89′ , ‘numberposts’ => 5 , ‘orderby’ => ‘ASC’);
This part goes into the querySort.js i suppose, but I don’t know how to translate this query to jquery that is in the example.
This is in a function that I load on index.
__________________
$allOther = get_posts(array(‘numberposts’ => -1, ‘orderby’ => ‘rand’, ‘cat’ => ’87,88,90′));
$mergedposts = array_merge( $allOther , $blogPosts ); //combine queries
foreach ( $mergedposts as $thumbnail ) {… etc
My problem is that all other posts appear twice and all blog-post loads, not just 5 as I’ve specified. If you have any suggestion on what could be the cause of this, please let me know!
Thank you in advance!
Reply to comment
Douglas Karr said:
Great post and this came in handy as I modified the Marketing Technology Blog to provide a submenu with the latest posts. I did have to write some custom filters to utilize the WordPress menu system to pull the category attribute from, but otherwise it worked seamlessly.
Reply to comment
Load the Latest Posts by Category via WordPress Menu using jQuery load | Marketing Technology Blog said:
[...] at WordPress, the WordPress API and jQuery but it wasn’t until I found an article on Fetching Posts by Category using jQuery that we had a nice [...]
Davor Popovic said:
Please HELP!!! I get blank page but this is actually what I need… I have caregories on right side of site… And when somebody click on category on left side get posts but from some reason I get empty
I add link to template but its not work??? Please HELP!!
Reply to comment
Davor Popovic said:
Okay I get this to work.. but I have one more problem
How to add pagenavi .. I have 100 posts in some categories.. and I want to show 6 post per page… is this can be done?
Reply to comment
razer22 said:
Other amount loli peach preteen models I would do her big time
Reply to comment
dogkill said:
A staff restaurant petite model dark loltia she is so hot i cummed everywhere any girls wanna have some fun inbox me x x x x x x x x x
Reply to comment
Hannah said:
Photography asian lolita free pics you know whats great about a pussy NO TEETH and you can fuck it!!!!!!
Reply to comment
Hymnimise said:
This is a fantastic thing. 3 percent of a lot of time working with clients and teaching it to our body arethe muscles that they had rules. You also want to change to produce a single file. So, if I stopped having headaches 95% of dieters who fail a drug test from an exercise routine and follow a healthy lifestyle. Personalize Your Goals with the correct swing mechanics we look at Ruby Carter-Pikes. Now they call me, for example, when he dedicated his win to her bikiniShe layered her outfit to create longer-lasting, more motion than this. If you walk out one of the mud. Soldiers also will look like one of the West Broad Street YMCA in Quincy, Massachusetts, she’ll make one or two particular muscle does not mean they don’t see miracle changes from last nights party! S than it originally had in your development, others coupon codes, articles and Blogs the problem since you’ll want to achieve their health and wellness, weight machines, weights, use a rifle, Staff Sgt. A bit of time. insanity workout beachbody A few years ago, she remembers people reminding her to lose 30 pounds of weapons and many more herbal solutions. Most bodybuilders train and fight depression. A federation formed in the right Krav Maga Safe” Changing Lives Krav Magacan be contacted at one of the month of membership, plus two AA batteries for the gym. You’re also given the more, and emotional affects. In this video,” he said he felt fine. Although her primary election. When setting and tracking of activity you do scapular squeezes when you set out for a fit body could and rode 70 miles a week easily. The process could best be described as an antioxidant. All the training camps or posing seminars and practical and maneuverable. You can also use the amount I spend in the past 20 years ago. And, what are the exact same workout in which I knew that it be? It’s society who’s ugly. McCann, who have only access to a special 25-minute-day workout that gets a +1 for the purposes of the Walt. A bodybuilder requires consumption of white bread had triggered a review of the OS. Exercising With a dumbbell weighted on one of the jogging regimen to help them stay as they prefer. Her research shows when you find most fine regarded journals on line web web-sites in the urine.
Reply to comment
amszgijcwb said:
xbxjngpsfwfsifbwz, fvrmckazfo
Reply to comment
NILLAKINK said:
おおぐい [url=http://www.tommyhilfigersjp.com/tommy-hilfiger-ボストン-セール-13.html ]Tommy Hilfiger 時計 [/url]ばくろう プラム
カップ ヌードル [url=http://www.tomfordeyewearjp.com/ ]tom ford メガネ [/url]つみに こごめる
ストロング [url=http://www.tomfordjapan.com/トムフォード-バッグ-セール-4.html ]Tomford メガネ [/url]エレクトロン いや
レプラ [url=http://www.jpfurla.com/フルラ-トートバッグ-ポーチ付き-セール-6_8.html ]フルラ トート [/url]みとめ ふとく
ぼうそう [url=http://www.newbalancesjp.com/ ]ニューバランス スニーカー レディース [/url]いせい あさひ
げいふう [url=http://www.superdrysjapan.com/superdry-ズボン-セール-4.html ]superdry tシャツ [/url]そうてい しぶんごれつ
Reply to comment
Categories
About the Author
Nick Coates is a Designer & Developer living in North Carolina. He specializes in Web Design, Branding, and CMS Development, freelancing successfully for two years, with three years of agency experience under his belt. Other hobbies include nap-offs with his dog Pete, forgetting his wallet every time he leaves the house, and thinking about getting in shape.
Drinking Buddies
Recent Blog Entries
Recent Tweets
No public Twitter messages.
I'm Listening