dsomm80482 2017-06-19 13:11
浏览 46

在选择菜单中删除重复的城镇

I have a select menu option where i get cities in this way:

select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc 

The problem is that there're towns which are displayed more than once for example "Berlin Berlin Berlin". How can i remove the duplicated towns using sql query or something ?

Here's the whole file:

public static function get_destination_countries() {
        global $wpdb;

        $countries = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = 0 order by post_title asc ", ARRAY_A );

        return $countries;
    }

    /**
     * Fetch all destination states by a country
     *
     * @return array all states by a country, array with post_ID and post_title
     */
    public static function get_destination_states( $country_id ) {
        global $wpdb;
        if(!empty($country_id)){
            // We need to make sure the destination state has ACF "state"
            $place_type_tax_id = '';
            $place_type_tax = get_term_by( 'slug', 'state', 'place-type' );
            if ( ! empty( $place_type_tax ) && ! is_wp_error( $place_type_tax ) ) {
                $place_type_tax_id = $place_type_tax->term_id;
            }

            //$states = $wpdb->get_results( "select ID, post_title from $wpdb->posts where post_type = 'place' and post_parent = $country_id order by post_title asc", ARRAY_A );
            $states = $wpdb->get_results( "select p.ID  , p.post_title, pm.meta_value from $wpdb->postmeta pm inner join $wpdb->posts p where p.post_parent = $country_id and p.ID = pm.post_id and meta_value = '$place_type_tax_id' order by post_title asc" , ARRAY_A );

            return $states;
            }
        return false;
    }

    /**
     * Get a list of all cities in a State
     */
    public static function get_cities_from_state( $state_id ) {
        global $wpdb;
        if(!empty($state_id)){
            $cities = $wpdb->get_col( "select ID from $wpdb->posts where post_type = 'place' and post_parent = $state_id order by post_title asc" );
            return $cities;
        }

        return false;
    }

    /**
     * Pull up a country ID by name - helpful to get United States as an ID in any DB
     */
    public static function get_destination_country_id_by_name( $country_name ) {
        global $wpdb;
        if(!empty($country_name)){
            $country_id = $wpdb->get_var( "select ID from $wpdb->posts where post_type = 'place' and post_title = '$country_name' order by post_title asc " );
            return $country_id;
        }

        return false;
    }

    /**
     * Pull destination's (countries, states etc) name by place ID
     */
    public static function get_destination_name_by_id( $place_id ) {
        global $wpdb;
        if(!empty($place_id)){
            $destination_name = $wpdb->get_var( "select post_title from $wpdb->posts where post_type = 'place' and ID = $place_id order by post_title asc" );
            return $destination_name;
        }

        return false;
    }

    /**
     * Fetch the country ID by state ID being a parent - used for the results page
     */
    public static function get_country_by_state_id( $place_id ) {
        global $wpdb;
        if(!empty($place_id)){
            $parent_id = $wpdb->get_var( "select post_parent from $wpdb->posts where post_type = 'place' and ID = $place_id order by post_title asc" );
            return $parent_id;
        }

        return false;
    }

    /**
     * Needed for the destination results page
     *
     * Fetch all destinations attached through ACF via state
     */
    public static function get_destination_ids_by_state( $state_id ) {

    }

    /**
     * Get destinations in a list of cities, TBD
     */
    public static function get_destinations_in_cities( $cities ) {
        global $wpdb;
        if(!empty($cities)){
            $cities = $wpdb->get_col( "select ID from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc" );
            return $cities;
        }
        return false;
    }
  • 写回答

2条回答 默认 最新

  • dongmu1951 2017-06-19 13:13
    关注

    Try:

    select DISTINCT(ID) from $wpdb->posts where post_type = 'place' and post_parent = $cities order by post_title asc
    

    DISTINCT will return the unique ID's from table.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。