• Resolved itcodeba

    (@itcodeba)


    Hi,

    I am having a problem with sorting. Here is the result of the <abc> sorting:

    • 10 bara (100 m / 330 ft) (127)
    • 20 bara (200 m / 660 ft) (12)
    • 3 bara (30 m / 100 ft) (7)
    • 30 bara (300 m / 1000 ft) (34)
    • 5 bara (50 m / 165 ft) (3)
    • 60 bara (600 m / 2000 ft) (5)

    However, I need it to be sorted as: 3/5/10…

    Can you help me with this issue?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support fesupportteam

    (@fesupportteam)

    Hi @itcodeba

    Only with the help of manual sorting can you write a custom code using the “wpc_terms_before_display” hook. More information on it can be found here – https://filtereverything.pro/resources/hooks/#terms-before-display-hook

    Best Regards – Victor

    Thread Starter itcodeba

    (@itcodeba)

    I solved it, here is the code if anyone has a similar problem

    add_filter('wpc_terms_before_display', 'wpc_sort_terms_by_vodootpornost', 10, 4);

    function wpc_sort_terms_by_vodootpornost($terms, $filter, $set, $urlManager) {
    // Provjerite da li je filter za željenu taxonomy
    if ($filter['e_name'] === 'vodootpornost_tax') {
    $newTerms = [];

    foreach ($terms as $term) {
    // Ekstrahirajte numeričku vrijednost iz naziva termina (npr. "10 bara")
    preg_match('/(\d+) bara/', $term->name, $match);
    $term->menu_order = isset($match[1]) ? intval($match[1]) : 0;
    $newTerms[] = $term;
    }

    // Sortiranje u rastućem redoslijedu
    usort($newTerms, function($a, $b) {
    return $a->menu_order - $b->menu_order;
    });

    return $newTerms;
    }

    return $terms;
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Issue with Sorting Numeric Values in Ascending Order’ is closed to new replies.