I am having trouble coming up with a solution for product catalog I am building using custom post types and 4 custom taxonomies. The product catalog is separated by "model", but each "model" can have up to 15 "product" variations. Each "product" has different attributes and I need to be able to filter a list of products based on these attributes.
Structurally, I have things organized like so:
- Product (Custom Post Type)
- Model (Taxonomy)
- Size (Taxonomy)
- Color (Taxonomy)
- Features (Taxonomy)
With the exception of "Model", each taxonomy is used as a way to filter the products shown. I am using the Query Multiple Taxonomies plugin to help me filter the attributes for each product. This works great. It allows me to use the default Wordpress template hierarchy and make all my changes to a taxonomy.php file.
Now here is where I get lost. I would like to limit the results that are shown so only 1 of each model is shown, even though the filtering may return multiple products per model.
I am currently using The Loop and get_the_terms() to create an array, and then using the array_unique() function to save only one instance of each model name to an array. Here is the code I have for that portion:
$modelSlugs = array(); while (have_posts()) : the_post(); // Gather the model names assigned to each product $terms = get_the_terms( $post->ID, 'models' ); if ( $terms && ! is_wp_error( $terms ) ) : $term = array_pop($terms); $modelSlugs = $term->slug; endif; endwhile; $modelList = array_unique($modelSlugs);
This will give me only one of each model slug (or name), which is good, but I also need to show a product thumbnail and use the permalink to link to the single product page. I know I can generate additional queries based on each slug, but that sounds like it is a performance nightmare.
The more I think about it, I can't help think this is the wrong way to go about this. I know the returned $wp_query object has everything I could possibly need, I am just not sure what tricks to use to ensure I only show one post for each model. Is there a way to prepare or manipulate the returned WP_Query object before running the loop, or is there something I can do inside of the loop to limit how many products are displayed?