WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display values for a given custom field meta_key for the current cateogry (9 posts)

  1. adambundy
    Member
    Posted 3 years ago #

    I'm trying to display a menu of the available values for a given meta_key (custom field). For instance, on a category page for products in the 'Mens Softshell Jackets' category, I want to show the available brand names (which I have stored as custom field values) for that category only, which I will then use to further restrict the display, but I can handle that part. Thanks much for any help!

  2. popper
    Member
    Posted 3 years ago #

    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;
    }
    ?>
  3. adambundy
    Member
    Posted 3 years ago #

    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!

  4. popper
    Member
    Posted 3 years ago #

    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.

  5. adambundy
    Member
    Posted 3 years ago #

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

  6. popper
    Member
    Posted 3 years ago #

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

  7. adambundy
    Member
    Posted 3 years ago #

    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!

  8. popper
    Member
    Posted 3 years ago #

    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.

  9. adambundy
    Member
    Posted 3 years ago #

    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 />");
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic