0

I am using a loop to display custom field values in a navigation area. Example: I have 100 posts on the page. Each post has a Brand associated with it (Fender, Gibson, etc.) maybe about 15 brands overall. The navigation loop will output the Brand custom field value for the posts. I want it to only show a brand once, so that instead of outputting 100 Brand values, the loop only outputs 15 unique values, with no duplicates. Everything google shows me about preventing duplicates has to do with wanting a second loop not to duplicate anything from the first loop. This is not my issue - I simply want to prevent duplicate output in a single loop. Any advice? Thank you.

<?php
$args=array('posts_per_page' => -1, 'post_status' => 'publish', 'orderby'=> 'title',     'order' => 'ASC', 'cat' => get_query_var( 'cat' ) );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {  ?>


<?php
while ($my_query->have_posts()) : $my_query->the_post();
?>

<h3 style="padding-left:10px;"><a href="<? the_permalink() ?>" title="<? the_title() ?>"><?php 
$key_1_value = get_post_meta( get_the_ID(), 'brand_value', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
  echo $key_1_value;
} 
?></a></h3>

<?php endwhile; ?>
<?php }
wp_reset_query();
?>
WilliamAlexander
  • 374
  • 2
  • 4
  • 20

1 Answers1

0

Create an empty array

In your loop, add the brand to this array. If the brand already exist in the array, then don't add it and don't output the post.

Pseudocode:

$args = array(
    'posts_per_page' => -1,
    'category' => 1,
    'order' => 'ASC',
    'orderby' => 'title'
);

$brand_list = array();
$posts_array = get_posts($args);

foreach( $posts_array as $post ) : setup_postdata($post);
  $brand = get_post_meta( $post->ID, 'brand_value', true);
  if(!in_array($brand,$brand_list){
    array_push($brand_list,$brand);

    echo 'brand: '.$brand;
    the_content();
  }
endforeach;
Steven
  • 19,224
  • 47
  • 152
  • 257
  • Well this looks like it will work. I'm not sure how to fold that into the query I'm using for that navigation. I've updated the post to add the query code I'm using - can you help me understand where I put the code you gave me? @Steven – WilliamAlexander Jul 08 '13 at 14:04
  • When I try to work this in I get an error about an unexpected { - but I'm kind of stabbing in the dark as I'm not 100% sure how to get this to fit into the query/loop I'm using. – WilliamAlexander Jul 08 '13 at 14:35
  • Try and work with my edited code. Just use correct category ID. – Steven Jul 08 '13 at 14:38
  • For some reason the error I'm getting is: Parse error: syntax error, unexpected '{' in blahblahblah/category-41.php on line 192 - it's the one after $brand_list) - any idea why that is? Also this is going to go onto a category page - is there a way for me to pass the current category id into that $args array? @Steven - thanks for your help. – WilliamAlexander Jul 08 '13 at 14:47
  • 1
    Yes, there is missing a `)`. I think you need to start with basics. Freshen up on your PHP and learn how WP / WP Loops works. – Steven Jul 08 '13 at 14:55
  • Yes I am definitely learning by doing as I haven't had an education in PHP or WP other than digging into it and trying to get things to work. I'm not sure exactly what I need to take out of the code I have and where I need to insert the code you've given me. Thank you for your help, I will keep working with it. Sorry for my lack of knowledge. – WilliamAlexander Jul 08 '13 at 14:58
  • I find two pages very usefull: http://www.smashingmagazine.com and http://net.tutsplus.com/. And I guess you already have started here: http://codex.wordpress.org/WordPress_Lessons. Good luck. – Steven Jul 08 '13 at 15:11