logo

$10
List values for a given meta_key in the current category

Im attempting to list on basic category pages (category.php) a list of all values for a certain meta_key ('product_brands' in this case) for the currently showing category. Any takers?

Im using this query to do the heavy lifting on this category template:


<?php

$sort= $_GET['sort'];

if($sort == "brand") {
// sort by brand;
$meta_key= "product_brand";
$orderby= "meta_value";
$order= "ASC";
} else {
// sort by price;
$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 .'');
?>

Adam Bundy | 07/23/10 at 5:22pm | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    07/23/10
    6:50pm
    Oleg Butuzov says:


    $posts = get_objects_in_term(array($YourCategoryID), 'category');
    $results = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in('.implode(",", $posts).')");
    var_dump($results);
    // your brends

    Previous versions of this answer: 07/23/10 at 5:29pm | 07/23/10 at 5:47pm

    • 07/23/10 5:30pm

      Oleg Butuzov says:

      $YourCategoryID can be replaced by the $wp_query->query_vars['cat'];

    • 07/23/10 6:02pm

      Adam Bundy says:

      Oleg- Ive tried quite a few variations on what you posted, including the following, but the return is always NULL.


      <?php
      $myPosts = get_objects_in_term(array($thiscategory), 'category');
      $myBrands = $wpdb->get_results("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in('.implode(",", $myPosts).')");
      var_dump($myBrands);
      ?>


    • 07/23/10 6:09pm

      Oleg Butuzov says:

      var_dump($wpdb);
      // do you have this variable?

    • 07/23/10 6:12pm

      Adam Bundy says:

      Yep, I got a load of output when I inserted that...

    • 07/23/10 6:16pm

      Oleg Butuzov says:

      step by step

      <?php

      $myPosts = get_objects_in_term(array($thiscategory), 'category');
      var_dump($myPosts);
      // should return the list of IDS of the posts...

      $sql = "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in('.implode(",", $myPosts).')";
      var_dump($sql);
      $myBrands = $wpdb->get_results($sql);
      var_dump($myBrands);
      ?>

      actualy you shoulnd get null, you should at elast get empty array...

    • 07/23/10 6:23pm

      Adam Bundy says:

      There seems to be something wrong with the second part- the first part outputs something like this:

      array(20) { [0]=> string(4) "2238" [1]=> string(4) "2239" [2]=> string(4) "2249" [3]=> string(4) "2250" [4]=> string(4) "2869" [5]=> string(4) "3405" [6]=> string(4) "3792" [7]=> string(4) "4043" [8]=> string(4) "4792" [9]=> string(4) "4815" [10]=> string(5) "21829" [11]=> string(5) "21987" [12]=> string(5) "24148" [13]=> string(5) "24156" [14]=> string(5) "24157" [15]=> string(5) "29710" [16]=> string(5) "31267" [17]=> string(5) "31712" [18]=> string(5) "35099" [19]=> string(5) "35117" }

      When I insert the second step, I get blank page...

    • 07/23/10 6:30pm

      Oleg Butuzov says:

      global $wpdb;
      and step 2

    • 07/23/10 6:34pm

      Adam Bundy says:

      OK now I have this, and still getting a blank page:

      <?php 

      $myPosts = get_objects_in_term(array($thiscategory), 'category');
      var_dump($myPosts);

      global $wpdb;

      $sql = "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in('.implode(",", $myPosts).')";
      var_dump($sql);

      //$myBrands = $wpdb->get_results($sql);
      //var_dump($myBrands);
      ?>

    • 07/23/10 6:37pm

      Oleg Butuzov says:

      can you give me an ftp access to debug it?

    • 07/23/10 6:38pm

      Oleg Butuzov says:

      oops!
      soorry correct string for sql


      $sql = "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in(".implode(",", $myPosts).")";

    • 07/23/10 6:42pm

      Adam Bundy says:

      Now we're getting somewhere! The dump for that gives me this:

      string(194) "SELECT meta_value FROM wp_postmeta WHERE meta_key = 'product_brands' AND post_id in(2238,2239,2249,2250,2869,3405,3792,4043,4792,4815,21829,21987,24148,24156,24157,29710,31267,31712,35099,35117)"

      But the dump for the $myBrands gives this:

      array(0) { }

    • 07/23/10 6:44pm

      Adam Bundy says:

      Found a goof- 'product-brands' should be 'product-brand'. That was the ticket. So, here's the final output:

      array(20) { [0]=> object(stdClass)#5588 (1) { ["meta_value"]=> string(9) "Big Agnes" } [1]=> object(stdClass)#5587 (1) { ["meta_value"]=> string(9) "Big Agnes" } [2]=> object(stdClass)#5586 (1) { ["meta_value"]=> string(9) "Big Agnes" } [3]=> object(stdClass)#5585 (1) { ["meta_value"]=> string(9) "Big Agnes" }...

      Oleg, could you help me echo this in a nicer list (comma sep.)?

    • 07/23/10 6:45pm

      Oleg Butuzov says:

      are you sure product_brands is your meta_key for the brends ?

      you can debug it by

      $sql = "SELECT meta_value, meta_key FROM {$wpdb->postmeta} WHERE AND post_id in(".implode(",", $myPosts).")";


      this will return all meta_keys and meta_values from the posts inside this category... is there product_brands?

    • 07/23/10 6:47pm

      Oleg Butuzov says:

      1) replace wpdb->get_results by wpdb->get_col
      2) and

      echo implode(', ', $results);


    • 07/23/10 6:47pm

      Oleg Butuzov says:

      and this is a better sql

      $sql = "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = 'product_brands' AND post_id in(".implode(",", $myPosts).") GROUP BY meta_value ";

    • 07/23/10 6:49pm

      Adam Bundy says:

      Take a bow, Oleg. Made my day. Have a great weekend!

  • avatar
    Last edited:
    07/23/10
    5:52pm
    Paul Sanduleac says:

    Nevermind...mine would not work for you.

    Previous versions of this answer: 07/23/10 at 5:52pm

This question has expired.





Current status of this question: Completed