• Resolved gomymusic

    (@gomymusic)


    Hey,

    How can I list all unique custom fields fomr a certain meta key from current category? I mean I have a custom meta key “product_brand” and I want to list all unique brands from the current category on the category page. I someone know?

    Thanks.

Viewing 15 replies - 1 through 15 (of 15 total)
  • vtxyzzy

    (@vtxyzzy)

    Here is some code that might help:

    $cat = get_query_var('cat');
    $meta_key = 'product_brand';
    $sql = "
    SELECT DISTINCT pm.meta_value FROM $wpdb->postmeta pm
    JOIN $wpdb->posts p ON p.ID = pm.post_id
    JOIN $wpdb->term_relationships tr ON  (p.ID = tr.object_id
             AND tr.term_taxonomy_id = $cat)
    WHERE pm.meta_key = '$meta_key'
    ORDER BY pm.meta_value ASC
    ";
    // print_r("<p>SQL:$sql</p>");
    $values = $wpdb->get_col($sql);
    //print_r($values);
    foreach ( $values as $brand ) {
       echo "<br />Brand: $brand";
    }
    Thread Starter gomymusic

    (@gomymusic)

    Yeah.. Thanks.

    But if I want to list all unique taxonomies from the current category in the category template page?

    Thanks.

    Sorry, I don’t understand exactly what you want.

    If the code I showed you is used in the category template page, it should list all the unique brands in the current category.

    Thread Starter gomymusic

    (@gomymusic)

    I want to list all taxonomy terms related to current category in category term. So.. If I access “category1” category page, I see all posts from that category. But I want to see all taxonomy terms that are associated only with post from the current category.

    PS: Right now I changed Brand from custom meta box in custom taxonomy.

    Thanks.

    Is that not what the code does?

    Thread Starter gomymusic

    (@gomymusic)

    I dont know what you mean

    I thought that the code I gave you would do what you want. What is different from what you wanted?

    Thread Starter gomymusic

    (@gomymusic)

    First time I want to list all meta keys from cusrrent category, right now I want to list all taxonomy terms from current category.

    OK I see now. Will work on some code.

    Thread Starter gomymusic

    (@gomymusic)

    Thanks a lot!!!

    Do you want all the category terms, or terms from all taxonomies?

    Thread Starter gomymusic

    (@gomymusic)

    terms from a certain taxonomy…
    I want to choose what taxonomy to be…

    Thread Starter gomymusic

    (@gomymusic)

    So.. I have:
    category1term, category2term
    taxonomy1term, taxonomy2term
    post1, post2, post3, post4

    When I see the category page category1term I see post1 and post2 (post3 and post4 are in other category). Both of them are in taxonomy1term. In this case I want to list only taxonomy1term because post1 and post2 are associated with taxonomy2term..

    ??

    I think this will do what you want (replace ‘madeof’ with the slug of your taxonomy):

    $cat = get_query_var('cat');
    $taxonomy = 'madeof';
    $sql = "
    SELECT DISTINCT t2.name
    FROM $wpdb->terms t2
    JOIN $wpdb->term_taxonomy tt2 ON (t2.term_id = tt2.term_id
          AND tt2.taxonomy = '$taxonomy')
    JOIN $wpdb->term_relationships tr2 ON (tt2.term_taxonomy_id = tr2.term_taxonomy_id)
    JOIN $wpdb->posts p ON tr2.object_id = p.ID
    JOIN $wpdb->term_relationships tr ON  (p.ID = tr.object_id
             AND tr.term_taxonomy_id = $cat)
    ORDER BY t2.name ASC
    ";
    // print_r("<p>SQL:$sql</p>");
    $values = $wpdb->get_col($sql);
    // print_r($values);
    foreach ( $values as $term ) {
       echo "<br />Brand: $term";
    }
    Thread Starter gomymusic

    (@gomymusic)

    It works.
    Thanks a lot.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘List all unique custom fields fomr a certain meta key from current category?’ is closed to new replies.