0

it's a catalog website for different product. I need a function that will get the category of the selected product, and get all the product from that category

Let see an example. A table, is in wood category and kitchen. So when display table, under the table i like all the other kitchen product listed as well as all the other wood product... it good for cross selling !...

how do i do that ?

1 Answer 1

2

You can use WP_Query's tax_query to do this. You would want the args (just on the tax_query, I'll leave the rest up to you, you have the documentation) to look something like this:

'tax_query' => array(
    'relation' => 'OR'
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => 'wood'
    ),
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => 'kitchen'
    )
),

That method will allow you to query as many types of posts as you like. Alternatively, you can have multiple WP_Querys and use wp_get_post_categories() to get the terms for a foreach to feed dynamically generated suggestions, each with its own WP_Query. It sorta depends how you want the output to look.

2
  • I will prefer to do a request, get all product that are wood and kitchen into an array, then add all the kitchen to this array, then all the wood to this array, the single out all the array, and get it to screen !
    – menardmam
    Feb 27, 2012 at 22:15
  • That would require multiple WP_Querys...is this question solved?
    – mor7ifer
    Feb 27, 2012 at 22:24

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.