6

I'm trying to figure out how to arrange tags on a post using a method other than alphabetically. For example, a post has the following tags; alpha, beta, charlie. The post will always organize tags in that order.

I'd like to control the way tags are displayed on a post. Instead of alphabetically, I'd like to order post tags by the order they were added to the post. For example; alpha (entered2nd), beta (entered1st), charlie(entered3rd) would be added to a post, but the tags would be listed in this order naturally, alpha, beta, charlie. I'd like them to be displayed in this order; beta, alpha, charlie

Is this possible? If yes, can you please direct me?

  • What do you mean by order they were entered on a post – Pieter Goosen Jan 10 '15 at 3:35
  • Pieter, I've edited to original question to try to clarify your question. Thanks for your help! – Mr. B Jan 10 '15 at 3:42
  • 1
    There is unfortunately no native way by which Wordpress can know which tag was entered first. It is like what was first, the hen or the egg. I'm not going to say that this is impossible, but it likely is from a pure php perspective. – Pieter Goosen Jan 10 '15 at 4:09
  • I believe a tag that can be ordered/sorted is just a term of a custom taxonomy. Custom taxonomy terms can be set to be hierarchal, but then again... it would work pretty much as categories do. – gdaniel Jan 10 '15 at 16:15
  • I have a custom piece of code that displays 'related' posts based on the '1st tag' of a post. I'd like to be able to control 'which' tag is listed 1st on a post. Any ideas? – Mr. B Jan 10 '15 at 16:44
4

One way to do it is to store the order of the tags as post meta data, using the action set_object_terms which happens to pass the tags in the order they appear on edit, eg in your "functions.php":

// Called in admin on updating terms - update our order meta.
add_action( 'set_object_terms', function ( $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids ) {
    if ( $taxonomy != 'post_tag' ) {
        return;
    }
    // Save in comma-separated string format - may be useful for MySQL sorting via FIND_IN_SET().
    update_post_meta( $object_id, '_tt_ids_order', implode( ',', $tt_ids ) );
}, 10, 6 );

// Reorder terms using our order meta.
function wpse174453_get_the_terms( $terms, $post_id, $taxonomy ) {
    if ( $taxonomy != 'post_tag' || ! $terms ) {
        return $terms;
    }
    if ( $ids = get_post_meta( $post_id, '_tt_ids_order', true ) ) {
        $ret = $term_idxs = array();
        // Map term_ids to term_taxonomy_ids.
        foreach ( $terms as $term_id => $term ) {
            $term_idxs[$term->term_taxonomy_id] = $term_id;
        }
        // Order by term_taxonomy_ids order meta data.
        foreach ( explode( ',', $ids ) as $id ) {
            if ( isset( $term_idxs[$id] ) ) {
                $ret[] = $terms[$term_idxs[$id]];
                unset($term_idxs[$id]);
            }
        }
        // In case our meta data is lacking.
        foreach ( $term_idxs as $term_id ) {
            $ret[] = $terms[$term_id];
        }
        return $ret;
    }
    return $terms;
}

// Called in front-end via the_tags() or related variations of.
add_filter( 'get_the_terms', 'wpse174453_get_the_terms', 10, 3 );

// Called on admin edit.
add_filter( 'terms_to_edit', function ( $terms_to_edit, $taxonomy ) {
    global $post;
    if ( ! isset( $post->ID ) || $taxonomy != 'post_tag' || ! $terms_to_edit ) {
        return $terms_to_edit;
    }
    // Ignore passed in term names and use cache just added by terms_to_edit().
    if ( $terms = get_object_term_cache( $post->ID, $taxonomy ) ) {
        $terms = wpse174453_get_the_terms( $terms, $post->ID, $taxonomy );
        $term_names = array();
        foreach ( $terms as $term ) {
            $term_names[] = $term->name;
        }
        $terms_to_edit = esc_attr( join( ',', $term_names ) );
    }
    return $terms_to_edit;
}, 10, 2 );
  • Thank you for this idea @bonger! I'm going to look into it in the next couple of days. To be hones, I'm a .php neophyte that knows just enough to hack things up. So I may have several questions. Thanks again! – Mr. B Jan 14 '15 at 0:19
  • Thanks! I was looking to find something for a custom food_tag taxonomy term. That works out of the box! – Adriano Monecchi Jan 24 '17 at 18:42
  • Any chances that function get an update? It seems to have stopped working on WP 5.2.2 – Adriano Monecchi Aug 22 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.