Closed marconii2002 closed 7 years ago
I have the same issue, any solution?
I have the same issue, any solution?
Hello @marconii2002 @obie73 @lironsher
This is not a bug. It's an unfortunate problem stemming from how the WP XML-RPC API handles terms. The term must already exist and its ID is required in the Term object in the Terms array when creating a new post. In your example, you have to create the term first and the term_id is required.
Example
Term t = new Term
{
Taxonomy = "post_tag",
Name = "test tag"
};
Post post = new Post
{
Id = postID,
PostType = "post",
PublishDateTime = postdate,
Content = content,
Terms = new Term[] { t }
};
using (var client = new WordPressClient(new WordPressSiteConfig
{
BaseUrl = "site",
Username = "xxxx",
Password = "xxxx",
BlogId = 1
}))
{
var term_id = client.NewTerm(post.Terms[0]);
post.Terms[0].Id = term_id;
client.EditPost(post);
}
Also - there is no way to search for terms by name via the API to check if they already exist so often times we have some messy code. For instance, you might want to use a tag that already exists. Here's a (ugly) snippet of code that will check to see if the tag already exists and grab the Id otherwise it will create one. Then it will add it to the post.
var client = new WordPressClient();
var t = new Term() { Taxonomy = "post_tag", Name = "test" }; // new up a term object to attach to post. The Name is the one we want to look for or create
var terms = client.GetTerms("post_tag", new TermFilter() { Search = t.Name }); // get term names from API searching for the one we want
if(terms == null) {
var term_id = client.NewTerm(t);
t.Id = term_id;
} else {
foreach(var term in terms) {
if(term.Name == t.Name) {
t.Id = term.Id; // the Term exists already, grab it's id and attach it to our object
}
}
if(t.Id == null) {
// oh nos the search results didnt have the exact one we are looking for. lets create!
t.Name = t.Name;
var term_id = client.NewTerm(t);
t.Id = term_id;
}
}
Post post = new Post
{
Title = "hello world",
PostType = "post",
PublishDateTime = DateTime.Now,
Content = "hello world",
Terms = new Term[] { t }
};
client.NewPost(post);
Hi Andy,
The above code throws an error at line
var terms = client.GetTerms("post_tag", new TermFilter() { Search = t.Name }); // get term names from API searching for the one we want
CookComputing.XmlRpcV2 exception Message: {"Not Found"}
Solved the problem with case-sensitive
var client = new WordPressClient(wordPressSiteConfig);
var t = new Term() { Taxonomy = "post_tag", Name = "News" }; // Это тег который мы ищем в списке тегов и если не находим его то создаём.
var terms = client.GetTerms("post_tag", new TermFilter() { Search = t.Name }); // Проверяем, создан ли тег
if(terms == null) {
t.Id = client.NewTerm(t);
} else {
foreach(var term in terms) {
t.Id = term.Id; //Берём первый тег который нашёл фильтр.
break;
}
}
if(t.Id == null) {
t.Id = client.NewTerm(t); //Создаём новый тег, если ничего не нашли
}
Post post = new Post
{
Title = "hello worl323",
PostType = "post",
PublishDateTime = DateTime.Now,
Content = "hello world",
Terms = new Term[] { t },
Status = "publish"
};
int id = Convert.ToInt32(client.NewPost(post));
return id + ";" + client.GetPost(id).Link + ";" + client.GetPost(id).Title + ";" + client.GetPost(id).Status;
How add several tags to post? (как добавить несколько тегов к посту?)
I have this code
Code works without error but tag is not added to post. Why ?