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

时间:2017-06-19 13:11:46

标签: php mysql sql wordpress

我有一个选择菜单选项,我以这种方式获得城市:

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

问题在于,有不止一次展示的城镇,例如柏林柏林柏林"。如何使用sql查询删除重复的城镇?

这是整个文件:

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 个答案:

答案 0 :(得分:0)

尝试:

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

DISTINCT将从表中返回唯一的ID's

答案 1 :(得分:0)

如果不在array()中的变量首先将它们放入数组中。并使用array_unique($town);($ town基于您的数组变量)

array_unique()将确保数组中的项目将返回没有重复值的新数组。

了解更多详细信息:http://php.net/manual/en/function.array-unique.php