Wordpress category order by custom field(done). How to output custom field only once

I create a catalog of companies by cities using the Wordpress categories. Categories are cities. Custom field is a region in which cities are included. For custom field used - Advanced Custom Fields For build custom taxanomy order used - WP Term Order

$terms = get_terms( 'catalog_categories', array(
    'depth'      => 1,
    'number'     => 100,
    'parent'     => 0,
    'orderby'    => 'taxon_name', // order array name
    'order'      => 'DESC',
    'hide_empty' => false,
    'meta_query' => array(
        'taxon_name' => array(
            'key' => 'taxon' // acf custom field name
        ) )
) );

foreach ($terms as $key => $object) {
    $region = get_field('taxon', $object);
    echo $region . ' ' . $object->name . '<br>';
}

I recive:
Wielkopolskie Poznań
Wielkopolskie Bojanowo
Mazoweckie Warszawa
Dolnoslaskie Wrocław
Dolnoslaskie Jawor

But i need:
Wielkopolskie
-- Poznań
-- Bojanowo
Mazoweckie
-- Warszawa
Dolnoslaskie
-- Wrocław
-- Jawor

Help please with the decision of this problem
UPDATE
*Help please add a link (slug) to the Wordpress category to the code Here we collect all the cities of one region:
$collection[$region][] = $object->name;
We need to add to each city a slug
$object->slug;
The slug we need to use it as value for option.

'<option value="' . **SLUG** . '" data-select2-id="'. $data_select_id . '-' .$i .'">' . $city . '</option>';


UPDATE:
*Working code
We added to - $collection[$region][] = $object->name; - $object->slug and combined them into an array
$collection[$region][] = array('name' => $object->name, 'slug' => $object->slug);
And for the output we use - $city['name'] and $city['slug']

<?php
$terms = get_terms( 'catalog_categories', array(
    'depth'      => 1,
    'number'     => 100,
    'parent'     => 0,
    'orderby'    => 'taxon_name', // order array name
    'order'      => 'DESC',
    'hide_empty' => false,
    'meta_query' => array(
        'taxon_name' => array(
            'key' => 'taxon' // acf custom field name
        ) )
) );

$collection = [];
foreach ($terms as $key => $object) {
    $region = get_field('taxon', $object);

    if (!isset($collection[$region])) {
        $collection[$region] = [];
    }

    $collection[$region][] = array('name' => $object->name, 'slug' => $object->slug);
}

$data_select_id = 1;
echo '<div>';
    foreach ($collection as $region => $cities) {

        $i = 0;

        echo '<div label="' . $region . '" data-select2-id="' . $data_select_id . '">'. $region;
            foreach ($cities as $city) {
                $i++;
                echo '<option value="' . $city['slug'] . '" data-select2-id="'. $data_select_id . '-' .$i .'">' . $city['name'] . '</option>';
            }
        echo '</div>'; 

        $data_select_id ++;

    }
echo '</div>'; 
;?>

Upvotes: 4

Views: 2255

Answers (1)

dferenc
dferenc

Reputation: 8126

You could use an additional array to sort out cities into regions like this:

$collection = [];
foreach ($terms as $key => $object) {
    $region = get_field('taxon', $object);

    if (!isset($collection[$region])) {
        $collection[$region] = [];
    }

    $collection[$region][] = $object->name;
}

foreach ($collection as $region => $cities) {
    echo $region . '<br/>';
    foreach ($cities as $city) {
        echo '--' . $city . '<br/>';
    }
}

Upvotes: 2

Related Questions