-1

Make catagory wordpress sort by group.

Array
(
[0] => WP_Term Object
    (
        [term_id] => 199
        [name] => 170
        [term_group] => 2
    )

[1] => WP_Term Object
    (
        [term_id] => 410
        [name] => 80s
        [term_group] => 2
    )

[2] => WP_Term Object
    (
        [term_id] => 66
        [name] => 8BIT
        [term_group] => 0
    )

[3] => WP_Term Object
    (
        [term_id] => 411
        [name] => adventure
        [term_group] => 0
    )
//... more then 100+

i focus term_group have create 12 group. 1 to 12

is not a group when use

$tags = get_tags(); //array above
foreach ($tags as $tag) {
    if ($tag->term_group==1) {
        echo "group 1";
        echo $tag->name;
    }

    if ($tag->term_group==2) {
        echo "group 2";
        echo $tag->name;
    }

    // .... until group 12
}

this array can output like

group 1 xxx aaa bbb

group 2 170 80s

group 3 ....

derHugo
  • 83,094
  • 9
  • 75
  • 115

2 Answers2

0

solve

$group = array();
foreach ( $tags as $value ) {
    $group[$value->term_group][] = $value;
}
echo "group 1";
foreach($group['1'] as $result) {
    echo $result->name;
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

I can't able to access your $tags array. Because it is in array of object format.

So I just developed the code. You may try like below.

<?php
$dataArray = array();
foreach ($tags as $tag) {
    for($i=1;$i<13;$i++){
        if ($tag->term_group==$i) {
            $dataArray[$i] = $tag;
            continue 2; 
        }
    }
}
// Just print this $dataArray and check this array contain the term_group sets.

for($i=1;$i<13;$i++){
    echo "group".$i;
    if($dataArray[$i]){
        foreach ($dataArray[$i] as $key => $value) {
             echo $value['term_group'];
        }
        echo "<br/>";
    }
}
?>

Thanks.

Ramya
  • 199
  • 10
  • add if for($i=1;$i<13;$i++){ echo "group".$i; if ($dataArray[$i]) { foreach ($dataArray[$i] as $key => $value) { print_r($value); } } echo "
    "; }
    – user3834265 Jan 05 '19 at 06:21