Support » Developing with WordPress » Programmatically add term contains Hashtag (#)

  • Resolved vinvin27

    (@vinvin27)


    Hello there,

    Im struggling with a stuff in my wordpress development.

    I have to add term name => C# but I also have term name => C

    When Im checking :

    term_exists( 'C#' , 'pa_languages' );
    it return me the ID of ‘C’ term.

    So I can’t add C# :/

    I don”t how to fix that. I don’t care if the slug of create term can’t contain hashtag but I have to create C# term name.
    Im sure I can do that because WordPress do it without problem.

    Any help ? πŸ™‚

Viewing 3 replies - 1 through 3 (of 3 total)
  • According to https://developer.wordpress.org/reference/functions/term_exists/
    the first parameter could be an ID, a slug, or a name. Since it can handle all three, you can’t use that function to compare a term name to what will match a term slug.

    You could try to query the database yourself, or use something to change non-alpha characters to alpha (like “Csharp” or something), so that the name won’t match the slug.

    Moderator bcworkz

    (@bcworkz)

    I cannot replicate your problem. I did use tags instead of a custom taxonomy, that shouldn’t matter. I have tags where name=>C, slug=>cee and name=>C#, slug=>c-sharp. term_exists() returns the proper IDs for both. I also deleted one, then added it back so that the associated row is before the other in one case and after the other in the other case. In other words, order in the table made no difference.

    Just for grins, I tried adding 'name' => 'C&_#35;' (without the underscore, the HTML entity name for a hash char), which displayed properly as C#, but term_exists() couldn’t deal with either ‘C#’ or ‘C&_#35;’. So don’t try HTML entity tricks with term names πŸ™‚

    Did you try deactivating plugins and using a default theme to see if that made any difference?

    Hey @joy you’re totally right πŸ˜€

    :v

    Thanks you very much

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Programmatically add term contains Hashtag (#)’ is closed to new replies.