1

I have a list posts by tag, but the relation is not relevant because first look for the posts with the first tags stored alphabetically, and not the first main tag, that is to say for me the order of the tags are key because the first tag is the most relevant. So when saving everything, it is sorted alphabetically and this does not help me, any idea how to alter this?

screenshot

2
  • 1
    It may be easier to create your own custom taxonomy than try to alter Core alphabetization. – WebElaine Apr 16 '20 at 21:18
  • 1
    Terms/Tags aren't ordered, any order you see in the editor is purely circumstantial, there is no order mechanism in the database for terms. If you want to give special meaning to a term, you need to do so explicitly, e.g. store the term ID of the significant term in post meta – Tom J Nowell Apr 16 '20 at 21:18
2

Terms/Tags aren't ordered.

To help make the terms easier to read, the editor will list them alphabetically, but that isn't because they're ordered alphabetically in the database. It's because a piece of javascript sorted the terms in the UI control.

There is no order, only the order you give them. Fundamentally terms have no order. Any order you see in the editor is purely circumstantial, there is no order mechanism in the database for terms.

If you want to give special meaning to a term, you need to do so explicitly, e.g. store the term ID of the significant term in post meta. You cannot rely on order, as terms do not have order.

1
  • Exactly, I think that this is not going to be able to at all from the CORE, because what wordpress does is that if a tag exists it is simply the same id in the relation of increased term the counter, and if it is a new tag then insert a new one tag with new id, but the order does not exist in itself. I cannot order by ID because I know that the last one is not always the one that I put as the first tag in the editor. I have to create a separate custom field to be able to make that possibility, thank you very much for the reply. – Lenin Zapata Apr 16 '20 at 22:08
0

I faced the same issue and saw that while there is the wp_term_relationships table, it doesn't get properly updated with the tags' order on post update. As a workaround, I hooked into the rest_after_insert_this-post_type to update the order myself. Here is a more detailed explanation of what I did, along with some code that seems to work fine so far.

3
  • Hello, I find the article you created interesting, I'm going to check it to see if it works for me, is this also compatible with the Classic Editor? – Lenin Zapata Oct 20 '20 at 2:50
  • I don't know if it works the same with the classic editor. My approach is specifically for Gutenberg and I've already tried it on a site, where it works fine. If you use the classic editor, though, you could try an existing plugin, which says that requires the classic editor, as it is not compatible with Gutenberg. – giorgos Oct 20 '20 at 13:43
  • I use Gutenberg, I just wanted to see the scope of your solution, but I already check – Lenin Zapata Oct 21 '20 at 18:14

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.