• This is a cool little thing I’m trying to work out on a site I’ve been modifying for a few weeks now. I have created a category page that outputs a sortable table of posts. The data in the table is coming from MetaData in the posts: Brand, Model, Year, Price, etc. I’ve got all of that working, and now I’m gradually trying to figure out how to build a sidebar navigation that displays the Brands that appear on the page and allows you to click a brand to show only the items in that category that are of that brand. Right now what I’m trying to do is to build a sidebar query that will output all of the Brand meta keys for the category, but that will only display a brand once, so even though the category may contain 50 items, if the items only come from 4 different brands, this sidebar query would list only 4 things – the four brands.

    I think I have a good start on this – I have a query that only shows the brands for the items in the category. The place where I’m stuck is how do I tell the query not to output the brand if it has already been output once before? The page is here:


    Effects Category on Fretmill site

    Ignore the fact that the table only shows a few entries – I haven’t gone through all of the posts to add the meta data that is called into the table yet.

    and the code for my sidebar query (which you can see at the top of the sidebar in unstyled fashion) is:

    <?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();
        $class = get_post_meta($post->ID, 'class', true);
        $year = get_post_meta($post->ID, 'year', true); ?>
    
    <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();
    ?>

    Thanks for any help! I know I saw this described once on a website, but google has not brought me back to that page again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter William Alexander

    (@williamalexander)

    I should add that I am obviously not educated with PHP and Queries, but I’m gradually learning, and the above code is definitely cobbled together – some might be incorrect or unnecessary, but it seems to be working so far.

    Thread Starter William Alexander

    (@williamalexander)

    This seems like it must be possible – anyone smarter than me out there have any advice?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to prevent duplicate output of query?’ is closed to new replies.