Hey there,
if I understood what you want, you probably just need to Query the posts for that category, and then loop trough them saving the values of the meta_key. Here’s a very basic idea:
<?
//get the posts for the cat
$posts = get_posts($query_string);
$meta_values = array();
foreach ($posts as $post) {
$meta = get_post_meta($post->ID, 'meta_key', true);
// if the value is repeated, we don't need to save it.
if (!(in_array( $meta, $meta_values))) {
$meta_values[]=get_post_meta($meta, true);
}
}
//print 'em out'
foreach($meta_values as $meta){
echo $meta;
}
?>
Julia, thanks much for this. I tried but didnt have success- can you help me ‘insulate’ this from the main loop or other loops on the page so that it wont disturb any of those? Thanks!
Hey adam, what you mean by insulate, exactly? This shouldn’t bother any loops, but you could if you need multiple loops, use any of these ideas Other than that, post your code here or -if it’s to long- in pastebin.
Julia, thanks for the response. This is the query Im using to do the main heavy lifting on my category pages:
<?php
$this_category = get_category($cat);
$thiscategory=$this_category->cat_ID;
$sort= $_GET['sort'];
if($sort == "brand") {
$meta_key= "product_brand";
$orderby= "meta_value";
$order= "ASC";
} else {
$meta_key= "Retail_Price";
$orderby= "meta_value_num";
$order= "DESC";
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('paged='.$paged.'&cat=' . $thiscategory . '&meta_key='. $meta_key .'&orderby='. $orderby .'&order='. $order .'');
?>
When I insert your code below this block, there is no output or error generated. Any ideas? Thanks!!
Ah, I see. Try this change:
$posts = get_posts('cat='.$thiscategory);
or you could use the same as above query string as above:
'paged='.$paged.'&cat=' . $thiscategory . '&meta_key='. $meta_key .'&orderby='. $orderby .'&order='. $order .''
Julia, thanks again but still no output. Doesnt this scenario require a wpdb function with a JOIN between two tables? I found this which is really close to what I need from what I understand, only I dont need the math- just a list of unique values:
http://wordpress.org/support/topic/341944?replies=22
Thanks!
You could do it both ways, I guess. But, I realized why the code won’t work for you, it’s wrong. Sorry! I edited it after testing it and left a typo. Anyway:
<?
//get the posts for the cat
$posts = get_posts($query_string);
$meta_values = array();
foreach ($posts as $post) {
$meta = get_post_meta($post->ID, 'meta_key', true);
// if the value is repeated, we don't need to save it.
if (!(in_array( $meta, $meta_values))) {
$meta_values[]=$meta;
}
}
//print 'em out'
foreach($meta_values as $meta){
echo $meta;
}
?>
replacing $query_string with yours and meta_key with the key you want.
I got this solved with the help of Oleg Butuzov. The following code did the trick:
<?php
$currentCatProds = get_objects_in_term(array($thiscategory), 'category');
global $wpdb;
$sql = "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brand' AND post_id in(".implode(",", $currentCatProds).")";
$myBrands = $wpdb->get_col($sql);
$myBrandsUnique = array_unique($myBrands); // remove duplicates
echo ("<strong>Brands in ");
echo $this_category->cat_name;
echo (": </strong>");
echo implode(', ', $myBrandsUnique);
echo ("<hr />");
?>