-1

I have below code that lists cities in ascending order, I want to lists cities based on the order I select the cities from the frontend. It is a shortcode created by theme. Cities is a custom taxonomy here.

How should I do this please advise?

Screenshot of City Listing in ASC order: https://prnt.sc/s87to8 backend city selection https://prnt.sc/s87gyi

view.php

<?php
if (!defined('FW')){
    die('Forbidden');
}
/**
 * @var $atts
 */

$title              = !empty( $atts['title'] ) ? $atts['title'] : '';
$description        = !empty( $atts['description'] ) ? $atts['description'] : '';
$cities             = !empty( $atts['city'] ) ? $atts['city'] : array();
$city_buttons       = !empty( $atts['city_buttons'] ) ? $atts['city_buttons'] : array();

?>
<?php if ( !empty( $cities ) ) { ?>
        <div class="tg-popularcities">                  
        <?php 
            $terms = get_terms( array(
                'taxonomy'      => 'cities',
                'include'       => $cities,
                'hide_empty'    => 0
            ) );

            if ( !empty( $terms ) ){                            
                foreach ( $terms as $key => $value ) {
                    $country_name = '';                  
                    $get_cities_meta = fw_get_db_term_option( $value->term_id, 'countries' );
                    if (!empty($get_cities_meta['country'][0])) {
                        $country_id = $get_cities_meta['country'][0];
                        $country_term = get_term($country_id, 'countries');
                        $country_name = $country_term->slug;                                                  
                    }

                    $city_name = $value->slug;  
                    $custom_url = "?country=".$country_name."&city=".$city_name;                            
                    $term_data = fw_get_db_term_option( $value->term_id, 'cities' );                        
                    $cat_image = !empty( $term_data['image']['url'] ) ? $term_data['image']['url'] : get_template_directory_uri().'/images/locations/city.jpg';
                    $total_users    = listingo_get_total_users_under_taxanomy($city_name,'number','city');
                ?>                                      
                <div class="col-xs-6 col-sm-6 col-md-6 col-lg-6">
                    <div class="tg-topcity">
                        <figure class="tg-cityimg">                                 
                            <img src="<?php echo esc_url( $cat_image ); ?>" alt="<?php esc_html_e('City Image', 'listingo'); ?>">                                   
                            <figcaption>                                        
                                <h3><a href="<?php echo esc_url( get_term_link( $value->term_id, 'cities' ) ); echo esc_attr( $custom_url  ); ?>"><?php echo esc_attr( $value->name ); ?></a></h3>
                                <span><?php echo esc_attr( $total_users ); ?>&nbsp;<?php esc_html_e('Listings', 'listingo'); ?></span>                                      
                            </figcaption>
                        </figure>
                    </div>
                </div>
            <?php } } ?>
        </div>
    <?php } ?>

options.php

<?php

if (!defined('FW'))
    die('Forbidden');

$options = array(
    'title' => array(
        'label' => esc_html__('Heading', 'listingo'),
        'desc' => esc_html__('Add section heading. leave it empty to hide.', 'listingo'),
        'type' => 'text',
    ),
    'description' => array(
        'type' => 'wp-editor',
        'label' => esc_html__('Description', 'listingo'),
        'desc' => esc_html__('Add section description. leave it empty to hide.', 'listingo'),
        'tinymce' => true,
        'media_buttons' => false,
        'teeny' => true,
        'wpautop' => false,
        'editor_css' => '',
        'reinit' => true,
        'size' => 'small', // small | large
        'editor_type' => 'tinymce',
        'editor_height' => 200
    ),
    'city' => array(
        'type' => 'multi-select',
        'label' => esc_html__('Select Cities', 'listingo'),
        'population' => 'taxonomy',
        'source' => 'cities',
        'prepopulate' => 500,
        'desc' => esc_html__('Show cities as per your selection. Leave it empty to show from all.', 'listingo'),
    ),
    'city_buttons' => array(
        'type' => 'addable-box',
        'label' => esc_html__('Add Button', 'listingo'),
        'desc' => esc_html__('', 'listingo'),
        'box-options' => array(
            'button_text' => array('type' => 'text'),
            'button_link' => array('type' => 'text'),
        ),
        'template' => '{{- button_text }}', // box title
        'box-controls' => array(// buttons next to (x) remove box button
            'control-id' => '<small class = "dashicons dashicons-smiley"></small>',
        ),
        'limit' => 1, // limit the number of boxes that can be added
        'add-button-text' => esc_html__('Add', 'listingo'),
        'sortable' => true,
    ),
);
Emma Expat
  • 57
  • 5
  • How are you selecting the cities from the front end? We need more information. What have you tried so far to do this? – disinfor Apr 29 '20 at 19:17
  • From the frontend https://prnt.sc/s87gyi I $terms = get_terms( array( 'taxonomy' => 'cities', 'include' => $cities, 'hide_empty' => 0 ) ); – Emma Expat Apr 29 '20 at 19:40
  • You said there is a short code on the front end. What Theme are you using? Does that short code output what is in your screen shot? You should add that screen shot to your question as well. What is the code in your question from? Is that in a template? – disinfor Apr 29 '20 at 19:46
  • https://prnt.sc/s87to8 My theme is Listingo – Emma Expat Apr 29 '20 at 19:52
  • This line fetches the cities name slected: $cities = !empty( $atts['city'] ) ? $atts['city'] : array(); It should list the cities in the same order as selected and saved in this array. – Emma Expat Apr 29 '20 at 20:00
  • @disinfor any suggestions to my query. – Emma Expat May 01 '20 at 07:15
  • @disinfor can you please check my response. – Emma Expat May 03 '20 at 19:38
  • Sorry for not getting back to you sooner! Looks like you got it figured out. Is that your code though, or part of the theme? If you update the theme, your changes will be overwritten. – disinfor May 04 '20 at 14:03
  • It's a shortcode inside my child theme – Emma Expat May 04 '20 at 14:16
  • Cool! As long as if you update the parent theme, and your child theme doesn't get updated, it's good. – disinfor May 04 '20 at 14:18

1 Answers1

0

By adding 'orderby' => 'term_order', line solved this issue.

$terms = get_terms( array(
                    'taxonomy'      => 'cities',
                    'include'       => $cities,
                    'orderby'  => 'term_order',
                    'order'    => 'ASC',
                    'hide_empty'    => 0
                ) );
Emma Expat
  • 57
  • 5