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.

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 . ', ';
endforeach;

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
add comment

1 Answer

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
add comment

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.