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

  1. ajmancilla
    Posted 3 years 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

    $states_var = $_GET['states'];
    $loop = new WP_Query( array( 'post_type' => 'doctors', 'posts_per_page' => 10 ) );
    while ( $loop->have_posts() ) { 
    		$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
    Posted 3 years ago #

    You can use SQL command's


    some examples at


    just focus on the SELECT DISTINCT one

  3. ajmancilla
    Posted 3 years ago #

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


  4. David Choi
    Posted 3 years ago #

    Wow, glad go it working after 2 weeks :)

Topic Closed

This topic has been closed to new replies.

About this Topic