• Resolved William Alexander

    (@williamalexander)


    Hi,

    I have this:

    <?php
    // The Query
    query_posts( 'post_type=fellow' );
    // The Loop
    while ( have_posts() ) : the_post();
    ?>
    <li><a href="#"><?php
    			$fellowship = get_post_meta( get_the_ID(), 'fellowship', true );
    			// check if the custom field has a value
    			if( ! empty( $fellowship ) ) {
    			echo $fellowship;
    			}
    ?></a></li>
    <?php
    endwhile;
    ?>

    I’d like to set this up so that a fellowship echos only once, no matter how many times it might show up in the query.

    I did this same thing a year ago using some different code, but I don’t think this was the best solution, it would be nice to be able to just use the query that’s already there and to do something like:

    if the $fellowship has already appeared, skip it.

    Here’s the code I used a year ago, in a totally different site:

    $args = array(
        'posts_per_page' => -1,
        'category' => get_query_var( 'cat' ),
        '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', true);
      if(!in_array($brand,$brand_list)){
        array_push($brand_list,$brand);
    
    $key_1_value = get_post_meta( get_the_ID(), 'brand', true );
    // check if the custom field has a value
    if( ! empty( $key_1_value ) ) {
    ?>
    <li class="menu-item menu-item-type-taxonomy menu-item-object-category"><a href="#" title="<? the_title() ?>" class="<?php
    $brandnav = get_post_meta(get_the_ID(), 'brand', true);
    $brandnav_array = explode(' ', $brandnav);
    $first_word = $brandnav_array[0];
    echo $first_word;
    ?>
    
    ">
    <?php
      echo $key_1_value;
    ?>
    
    </a></li>
    <?php
    } 
    
      }
    endforeach;
    
    ?>

    Any ideas? Thank you.

    https://192.185.31.23/~frontpor/wordpress/?page_id=15#

    William in Roanoke, Va

Viewing 5 replies - 1 through 5 (of 5 total)
  • Give this a try (untested):

    // The Loop
    $seen_fellowships = array();
    while ( have_posts() ) : the_post();
       $fellowship = get_post_meta( get_the_ID(), 'fellowship', true );
       // check if the custom field has a value
       if( ! empty( $fellowship ) && ! array_key_exists($fellowship, $seen_fellowships) ) {
          $seen_fellowships[$fellowship] = 1;
          ?>
          <li><a href="#"><?php echo $fellowship; ?></a></li>
       <?php }
    endwhile;
    Thread Starter William Alexander

    (@williamalexander)

    You nailed it! Thank you so much! Works perfectly. Now let me study this and see if I can understand how you did it. Awesome, awesome help. Thanks!

    Thread Starter William Alexander

    (@williamalexander)

    Not sure if you are interested in helping me understand what you did, but I think I generally understand everything except this line:

    $seen_focus_area[$focus_area] = 1;

    I imagine it is what allows the variable to be printed once, while ! array_key_exists is keeping it from being printed multiple times, but I’m not sure what that one particular line is going. Thanks so much for helping me out with this, great solution. Very simple.

    // The Loop
    // Set up an empty array with no keys
    $seen_fellowships = array();
    while ( have_posts() ) : the_post();
       $fellowship = get_post_meta( get_the_ID(), 'fellowship', true );
       // check if the custom field has a value
       // and the $fellowship key is not in the array
       if( ! empty( $fellowship ) && ! array_key_exists($fellowship, $seen_fellowships) ) {
          // Add the $fellowship key with a value of 1
          // Doesn't have to be 1, could be any true value
          $seen_fellowships[$fellowship] = 1;
          ?>
          <li><a href="#"><?php echo $fellowship; ?></a></li>
       <?php }
    endwhile;
    Thread Starter William Alexander

    (@williamalexander)

    Ok that makes sense – thanks so much for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to: Do not print duplicate entries from query?’ is closed to new replies.