[wp-hackers] Filter Posts by Custom Taxonomy with AJAX Select Lists
Aris Blevins
aris at goteama.com
Fri Sep 28 00:31:19 UTC 2012
I am working on a fairly complicated filtering 'search' page for a site that has over 3500 posts and a large, active, user base. This search will likely become a primary page for any site user. The site is on a VPS, and runs fine, but the owners try and keep their memory needs down to a minimum to reduce costs as they are a non-profit.
I have three custom taxonomies: Artists, Years and Genres that are associated with the posts. The page displays three multi-select boxes - one for each taxonomy. Users can select as many of the terms in each as they like, they then click to search and AJAX returns the HTML to the page for anything found.
That part works fine. The issue is that there is a high potential for null searchs, primarily because of too many terms used. Most of the artists have well under 100 posts, whereas years and genres can have a couple hundred at most. Selecting an artist and then a year or genre will usually result in a a zero set. Having users pound the database for null sets seems foolish.
My hope was that I could have data attributes in the select options - one for post counts (already provided in my term objects) and one for the IDs of posts in that term. This would serve two purposes:
- As users choose terms I can display how many posts for the taxonomy (These artists have 5 posts) Seeing that there is a small set of posts would stop users from feeling the need to add a bunch more terms to limit the results.
- The IDs could potentially be used to determine if the selected terms contain any common posts, if so, how many.
The problem is that the only way I can find to get the ids of the posts in a term is by running a new WP_Query for each term. I had hoped to use get_objects_in_term but that returns more than posts. It takes quite a while to build the page on first load.
My hope is that with caching the page (and all the data) would load rapidly, and that some fancy javascript responses will prevent users from searching for nulls and keeping my server a bit happier. Is this a good trade off? Is there a more efficient way to do this? Is there some other approach I am overlooking?
Happy to provide code if needed.
Thanks for any opinions or information.
Aris
More information about the wp-hackers
mailing list