WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this code:

global $wpdb;
$query = "SELECT DISTINCT meta_value 
                 FROM $wpdb->postmeta 
                 WHERE meta_key = 'product_brand' 
                 ORDER BY meta_value;";
$directors = $wpdb->get_results( $query );  
foreach( $directors as $director ) :
    echo $director->meta_value . ', ';

This code will show all meta values having the meta_key "product_brand". On category pages, what I want to do is list all unique meta values having the meta_key "product_brand" from the current category.

share|improve this question

what you could do is: 1. get the list of all posts currently on the page 2. grab all the IDs of the posts 3. do the following query:

SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'product_brand' AND post_id IN (3,9,10,15) ORDER BY meta_value;

where 3,9,10,15 are post ids that you've collected.

share|improve this answer

Your Answer


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.