WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] List all unique custom fields fomr a certain meta key from current category?

[Resolved] List all unique custom fields fomr a certain meta key from current category?

  • 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)
  • 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";
    }

    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.

    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?

    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?

    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.

    Thanks a lot!!!

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

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

    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";
    }

    It works.
    Thanks a lot.

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