1

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.

2
  • I have done something similar a while back, feel free to search through my answers, good luck ;-) Commented Jul 6, 2016 at 17:54
  • @PieterGoosen thanks for your reply. You have so many answers and I think I found which one you were talking about. The question was indeed about taxonomies vs categories but I don't think the asker wanted to achieve something similar to what I'm trying. Maybe I'm wrong?
    – Breus
    Commented Jul 6, 2016 at 19:03

1 Answer 1

0

I managed to do it. I used the following query parameter:

'tax_query' => array( array('taxonomy' => 'wpccategories', 'field' => 'slug', 'terms' => $termz ) )

With $termz being: $termz = wp_get_object_terms($id, 'wpccategories', array("fields" => "slugs"));

The problem I struggled with was that I needed to use singular 'field'with singular 'slug' in the tax_query (I knew this) but I needed to use plurar in the wp_get_object_terms function. So the other way around of any other kind doesn't seem to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.