• Resolved CAT the Tech

    (@catthetech)


    I found that there was no natural sort to this plugin, and decided to implement it myself.
    Please note that I have the free version of this plugin.

    <plugin_dir> refers to the location of the filter-everything plugin, in my case /var/www/html/wp-content/plugins/filter-everything

    First, add these 2 lines to <plugin_dir>/src/Admin/FilterFields.php‘s getOrderByOptions function:
    'naturalasc' => esc_html__( 'Natural &laquo;123&raquo;', 'filter-everything' ),
    'naturaldesc' => esc_html__( 'Natural &laquo;123&raquo;', 'filter-everything' ),
    This will add the options in the user’s Sort Terms by dropdown menu.

    Add the following case statements to <plugin_dir>/src/Entities/EntityManager.php‘s sortTerms‘s function’s switch statement:

            case 'naturalasc':
    usort( $terms, self::compareNatAsc('name') );
    break;
            case 'naturaldesc':
    usort( $terms, self::compareNatDesc('name') );
    break;

    This will point naturalasc and naturaldesc to 2 new function, see next step.

    Add these 2 new functions to <plugin_dir>/src/Entities/EntityManager.php‘s sortTerms‘s function:

    public static function compareNatAsc( $key ){
    
        return function ($a, $b) use ($key) {
            $value_1 = isset( $a->$key ) ? strtolower($a->$key) : 0;
            $value_2 = isset( $b->$key ) ? strtolower($b->$key) : 0;
    
            return strnatcmp( $value_1, $value_2 );
        };
    }
    
    public static function compareNatDesc( $key ){
    
        return function ($a, $b) use ($key) {
            $value_1 = isset( $a->$key ) ? strtolower($a->$key) : 0;
            $value_2 = isset( $b->$key ) ? strtolower($b->$key) : 0;
    
            return -strnatcmp( $value_1, $value_2 );
        };
    }

    Please update your code with this, this is a necessary sorting option for me and probably many other.
    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.