Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Given the following the following products:

product 1

  • Naam
  • part of taxonomy "brand" value "Joolz"
  • part of taxonomy "Categorie" value "Chair"

product 2

  • Naam
  • part of taxonomy "brand" value "Joolz"
  • part of taxonomy "Categorie" value "Car"

product 3

  • Naam
  • part of taxonomy "brand" value "Costa"
  • part of taxonomy "Categorie" value "Bike"

How could I want to retrieve all the categorie value that has products related to them with a given Brand, So I give value "Costa" I get bike, if i give value "Joolz" i get "chair" and "car"

share|improve this question
    
Bram, I'm sure I just did something similar to what you are requesting but I think your question needs a little more clarification. Precisely, why would brand "Joolz" give you both a chair and bike? From your example above product 1 and 2 appear to be the same. Please offer more information. Thnx. –  Brent Aug 27 '12 at 18:26
    
oh sorry wrong example's edited, would appreciate it a ton if you could share what you made, if its indeed what I need. –  Bram Aug 27 '12 at 18:36
    
Let me make sure I understand what you are asking. You have two different taxonomies: "Brand" and "Categorie". On any given product, both taxonomies exist. What you would like to do is to return the available "Categorie" taxonomies for any product that has a specific "Brand" taxonomy. As your example states, you only want to know what categorie are assigned to Products with a brand of "Joolz". Is that correct? –  Brent Aug 27 '12 at 18:51
    
Yea I want all the categorie taxonomy terms that has products with the brand Joolz. Lets say I have 1000 products with different brands and categories. Now I want to be able to pull all the categories that a certain brand has product for. So I can make a brand template page that for example says : 'Overview for all Joolz chairs, Joolz cars etc', but if Joolz doesnt make Bikes and hence has no producs in the car categorie I dont want to echo it. I want to create an array that has all the categorie name a brand has products in so I can loop thru it and echo it with the brand term in front of it. –  Bram Aug 27 '12 at 19:00
add comment

1 Answer 1

up vote 2 down vote accepted

Start with a tax_query to get posts with the 'categorie' taxonomy term you want. Assuming a custom query:

$tax_query_args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'categorie',
            'field' => 'slug',
            'terms' => array( 'costa' )
        ),
);
$tax_query = new WP_Query( $tax_query_args );

Then, step through that query, and use get_the_terms() to return the brand taxonomy terms for each post, and add them to an array:

$tax_query_brand_terms = array();

if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();

    $post_brand_terms = get_the_terms( get_the_ID(), 'brand' );
    if ( is_array( $post_brand_terms ) ) {
        $tax_query_brand_terms = array_merge( $tax_query_brand_terms, $post_brand_terms );
    }

endwhile; endif;
wp_reset_postdata();

Now, the $tax_query_brand_terms array should be populated with all of the brand taxonomy terms for posts in the specified categorie taxonomy term.

If you want to wrap it in a function, allowing you to pass a categorie taxonomy term as a parameter:

function wpse63233_tax_query_brand_terms( $categorie = false ) {
    if ( false == $catgorie ) {
        return false;
    }
    // Also, put in some error checking here,
    // e.g. comparing $categorie against
    // get_terms( 'categorie' )

    // Query 'categorie' posts
    $tax_query_args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'categorie',
                'field' => 'slug',
                'terms' => array( $categorie )
            ),
    );
    $tax_query = new WP_Query( $tax_query_args );

    // Get 'brand' terms from all returned posts
    $tax_query_brand_terms = array();

    if ( $tax_query->have_posts() ) : while ( $tax_query->have_posts() ) : $tax_query->the_post();

        $post_brand_terms = get_the_terms( get_the_ID(), 'brand' );
        if ( is_array( $post_brand_terms ) ) {
            $tax_query_brand_terms = array_merge( $tax_query_brand_terms, $post_brand_terms );
        }

    endwhile; endif;
    wp_reset_postdata();

    // Return the array
    return $tax_query_brand_terms;
}
share|improve this answer
    
Shouldnt the array ('costa') be the variable and categorie fixed? I want to place this in a brand template so it gets the brand of the specific page and then looks up all the associated categories –  Bram Aug 27 '12 at 19:14
    
Yes, sorry; I forgot to add the variable to the query args. Updated/fixed now. –  Chip Bennett Aug 27 '12 at 19:15
    
Changed categorie and brand to what its in my database, (pa_subcategorie and pa_merk) added echo of the function in merk taxonomy template while passing it a brand i know exist and is present in different subcategories so echo wpse63233_tax_query_brand_terms( 'Joolz' ) And doesnt return anything, I know I should be looping thru the array but normally this way should at least echo array, but it echoes nothing –  Bram Aug 27 '12 at 19:35
    
What do you get from var_dump( $tax_query)? –  Chip Bennett Aug 27 '12 at 20:06
    
i do $value = wpse63233_tax_query_brand_terms( 'joolz' ) echo joolz[0] This shoudl at least echo an array not? –  Bram Aug 27 '12 at 20:24
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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