Remove alphabetical order from wordpress categories

I am new to programming and WordPress.

I am working on a project where I am using a custom taxonomy (tag). When I select my tag in the admin panel (post), it automatically gets arranged in alphabetical order.

Read More

I’d like to stop that behavior, as I want to display the tags in the order I selected them.

Related posts

2 comments

  1. Here you go: just place this in your functions.php:

    /**
     * Sort post_tags by term_order
     *
     * @param array $terms array of objects to be replaced with sorted list
     * @param integer $id post id
     * @param string $taxonomy only 'post_tag' is changed.
     * @return array of objects
     */
    function plugin_get_the_ordered_terms ( $terms, $id, $taxonomy ) {
        if ( 'post_tag' != $taxonomy ) // only ordering tags for now but could add other taxonomies here.
            return $terms;
    
        $terms = wp_cache_get($id, "{$taxonomy}_relationships_sorted");
        if ( false === $terms ) {
            $terms = wp_get_object_terms( $id, $taxonomy, array( 'orderby' => 'term_order' ) );
            wp_cache_add($id, $terms, $taxonomy . '_relationships_sorted');
        }
    
        return $terms;
    }
    
    add_filter( 'get_the_terms', 'plugin_get_the_ordered_terms' , 10, 4 );
    
    /**
     * Adds sorting by term_order to post_tag by doing a partial register replacing
     * the default
     */
    function plugin_register_sorted_post_tag () {
        register_taxonomy( 'post_tag', 'post', array( 'sort' => true, 'args' => array( 'orderby' => 'term_order' ) ) );
    }
    
    add_action( 'init', 'plugin_register_sorted_post_tag' );
    

    (Credit goes to lgedeon on the WordPress Core Trac)

    Now all you need to do is enter the tags in the order you want them to appear.

    Note: the above code shows how to do this for post_tag. If you want a different taxonomy, just update the above code with the taxonomy name you want.

  2. When displaying categories you should use

    <?php wp_list_categories(array('orderby' => 'ID')); ?>
    

    Instead of ID you can use name,slug,count or term_group. See Codex

Comments are closed.