So I'm working with a bought version of a plugin (WP Catalogue, sorry I can't post more links than 2) as basis for my code for a website i'm working on and i'm adjusting it so that it can be used for a website I try to make. The thing is, instead of the default categories
as fields the writer of the plugin wrote a taxonomy wpccategories
for the categories of the posts. So my posts are all products of a shop and I want to show alternatives of the product to the customer. I use a query that queries 3 other products out of the same wpccategories
so with the same term->slug
as the term->slug
of the current product excluding the product itself of course. Otherwise the alternative of a product would be the product itself.
I finished making this and everything works perfectly fine (will show the code underneath) However, most products are in more than one wpccategories
and some of those categories have less than 3 products to show as alternatives. So what I want is that if lets say the first wpccategories
doesn't have enough products to show as alternatives, the system looks at the second (etc.) wpccategories
to search for alternatives.
This is how I made it right now (PHP):
global $post;
$id1 -> current_id;
$terms = get_the_terms($id1, 'wpccategories');
$exclude = array($id);
foreach ($terms as $term) {
}
$args = array(
'taxonomy' => 'wpccategories',
'post_type'=> 'wpcproduct',
'post__not_in' => $exclude,
'term' => $term->slug,
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => 3,
'paged' => $paged,
);
products = new WP_Query($args);
while($products->have_posts()): $products->the_post();
After this I assign all the fields to variables and use them to generate it on the website but I think you know how that works.
So my question is, how on earth do I fix it so that instead of just 'term'=> $term->slug
I get something like 'terms'=> something
or as I tried already with the 'tax_query' => array(array(foo))
as described in https://codex.wordpress.org/Class_Reference/WP_Query . Somehow I just can't manage to do it and everything I tried so far just gives posts out of all terms of all posts instead of all terms of the current post.
Another little thing, there is an empty foreach statement in my code: foreach ($terms as $term) { }
Is there a neater way to do this? Searched on Google but couldn't find anything.
If you can't visualize what i'm working on right now, check: http://www.bgc-testomgeving.nl/sem/
Any help is much appreciated because a bit stuck.