• Resolved janbrokes

    (@janbrokes)


    Hello
    I am displaing tags of each post in its detail. By default it is displaing tags order alphabetically, but it does not work with numbers

    see screen https://snag.gy/ZgnDy3.jpg

    I am using this code <?php the_tags( '??astnych 10 ta?ená ?ísla: ', ' ' , $after ); ?>

    Have you an idea how to change order of post tags from lowest number to highest?
    Thanks in advance

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,
    https://chrisltd.com/blog/2014/09/sorting-wordpress-taxonomy-terms
    I guess above link can give you a solution.
    thanks.

    Moderator bcworkz

    (@bcworkz)

    ThemesGrove’s resource that suggests using usort() is perfectly acceptable and will do the job. It’s often more efficient though to let SQL order the results as desired instead of using PHP to sort after the query is run. In the case of post tags I doubt one way or the other will make any noticeable difference, so altering the SQL query to order as desired is mainly of academic interest in this case.

    the_tags() function relies upon the WP_Term_Query class to get post tags from the DB. We can alter the SQL query that is run to get post tags with the “terms_clauses” filter. In the passed array of SQL clauses, we can alter the ORDER BY clause to sort naturally instead of alphabetically. By default the_tags() already sorts alphabetically by tag name. To cause the name field to be treated numerically, we can mathematically add zero to the name field value to cause it to be cast as an integer value.

    add_filter('terms_clauses', 'bcw_order_numeric', 10, 3 );
    function bcw_order_numeric( $clauses, $taxonomy, $args ) {
    	$clauses['orderby'] = 'ORDER BY t.name+0, t.name';
    	return $clauses;
    }

    This code, added to the theme’s functions.php, will cause all uses of WP_Term_Query to be natural sorted by name. If you need to limit this only to the particular call to the_tags(), don’t add the filter in functions.php, instead add the filter just before starting the loop that uses the_tags(), then remove the filter after the loop completes.

    The t. as the table reference is an alias established in the query’s FROM clause for the terms table. We add t.name after t.name+0 so that tag names with no numbers will be still be sorted alphabetically.

    Thread Starter janbrokes

    (@janbrokes)

    @bcworks – you are genius ?? it works! thank you very much

    Moderator bcworkz

    (@bcworkz)

    You’re welcome! I can’t take credit for adding zero to cast as an integer. That truly is genius, but not my idea. Beware though, it doesn’t always work as expected. Numbers preceded with alphabetic chars and zero aren’t properly handled. I got ordering similar to “foobar1, foobar5, foobar03, foobar04, foobar10, foobar16”

    It makes sense in that zero comes before 1 but after ‘r’, but it’s numerically wrong. But if you are consistently using preceding zeros, normal alphabetic ordering should work.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post tags order from lowest to highest’ is closed to new replies.