Search code examples
c#wordpress-rest-api

Search Unique Tag Name


I have one array of tags and need to check if the tag already exists in database.

The below code works well, however , queryBuilder.Search = t[i].Name works the same way as the LIKE in SQL. I mean, if I have too similar tags, ex "first tag is the best" and "first tag" will get error.

How to use the queryBuilder.Search to get exact match?

var queryBuilder = new TagsQueryBuilder();

for (int i = 0; i < t.Count(); i++)
{
    queryBuilder.Search = t[i].Name;

    var tags = await client.Tags.Query(queryBuilder);

    if (tags.Count() == 0) //If none in BD
    {
        var createdTag = await client.Tags.Create(t[i]);

        t[i].Id = createdTag.Id;
    }
}

Solution

  • As far as I get it WordPressPCL does not support it, but you can inherit from TagsQueryBuilder:

    public class ExtendedTagsQueryBuilder : TagsQueryBuilder
    {
      [QueryText("exact")]
      public bool Exact { get; set; }
    }
    

    And specify this parameter:

     ExtendedTagsQueryBuilder queryBuilder = new ExtendedTagsQueryBuilder();
     queryBuilder.Exact = true;
     queryBuilder.Search = t[i].Name;
     ...