WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying custom post type field, but displaying the repeating ones only once. (4 posts)

  1. ajmancilla
    Member
    Posted 10 months ago #

    Hi everyone,

    Hope someone can help me here.

    I am using WP Types plugin to create custom post types. so the thing is i have a post type with doctors and their city and state as custom fields.

    so from another post type page i am trying to populate all the cities within a state.

    So far i am able to pull the cities corresponding to that state but since i have several doctors in a city the cities repeat themselves.

    How can i prevent this to happen so it only displays the city one time?

    this is my code

    <?php 
    
    $states_var = $_GET['states'];
    
    $loop = new WP_Query( array( 'post_type' => 'doctors', 'posts_per_page' => 10 ) );
    
    while ( $loop->have_posts() ) { 
    
    		$loop->the_post();
    
    		$cities = get_post_meta($post->ID,'wpcf-city');
    		$states = get_post_meta($post->ID,'wpcf-state');
    
    		foreach($states as $state){
    
    				if($state == $states_var){
    
    						foreach ($cities as $city) {
    
    									echo "<a href=\"?cities=main&city=".$city."\">".$city."</a><br />";
    
    						}
    				}
    		}
    
    }
    ?>

    [No bumping, thank you.]

  2. David Choi
    Member
    Posted 10 months ago #

    You can use SQL command's

    SELECT DISTINCT

    some examples at

    http://wordpress.org/support/topic/how-to-run-a-wp_query-on-both-wp_posts-and-wp_postmeta-simultaneously?replies=14

    just focus on the SELECT DISTINCT one

  3. ajmancilla
    Member
    Posted 10 months ago #

    Hi just wanted to thank you, I got it working.

    cheers

  4. David Choi
    Member
    Posted 10 months ago #

    Wow, glad go it working after 2 weeks :)

Reply

You must log in to post.

About this Topic