WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] List all unique custom fields fomr a certain meta key from current category? (16 posts)

  1. gomymusic
    Member
    Posted 1 year ago #

    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.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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";
    }
  3. gomymusic
    Member
    Posted 1 year ago #

    Yeah.. Thanks.

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

    Thanks.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  5. gomymusic
    Member
    Posted 1 year ago #

    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.

  6. vtxyzzy
    Member
    Posted 1 year ago #

    Is that not what the code does?

  7. gomymusic
    Member
    Posted 1 year ago #

    I dont know what you mean

  8. vtxyzzy
    Member
    Posted 1 year ago #

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

  9. gomymusic
    Member
    Posted 1 year ago #

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

  10. vtxyzzy
    Member
    Posted 1 year ago #

    OK I see now. Will work on some code.

  11. gomymusic
    Member
    Posted 1 year ago #

    Thanks a lot!!!

  12. vtxyzzy
    Member
    Posted 1 year ago #

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

  13. gomymusic
    Member
    Posted 1 year ago #

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

  14. gomymusic
    Member
    Posted 1 year ago #

    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..

    :)

  15. vtxyzzy
    Member
    Posted 1 year ago #

    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";
    }
  16. gomymusic
    Member
    Posted 1 year ago #

    It works.
    Thanks a lot.

Topic Closed

This topic has been closed to new replies.

About this Topic