Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relation btw Post and Category #48

Closed
tayfunyasar opened this issue May 16, 2015 · 9 comments
Closed

Relation btw Post and Category #48

tayfunyasar opened this issue May 16, 2015 · 9 comments

Comments

@tayfunyasar
Copy link

How to set Post's Category?

i cant find categoryId in "Post" class.

Thanks

@KeesCBakker
Copy link
Collaborator

You can create a Term of the right type and as it to the Terms. Categories and Tags work through terms.

@tayfunyasar
Copy link
Author

How? Cant figure out

@KeesCBakker
Copy link
Collaborator

I use the following code:

var post = ...;

//create the keyword
var keyword = new Term
{
    Name = "My amazing new category",
    Slug = "miauw",
    Taxonomy = "category"
};

//save it
keyword.Id = client.NewTerm(keyword);

//create a terms list
var terms = new List<Term>();
terms.Add(keyword);

//add it
post.Terms = terms.ToArray();

Hope you'll get somewhere with it.

@KeesCBakker
Copy link
Collaborator

Both tags and categories are expressed as terms.

@tayfunyasar
Copy link
Author

Thanks! it works.

@KeesCBakker
Copy link
Collaborator

Great to hear. Sorry for the late reply. Has been pretty busy lately.

@cenaav
Copy link

cenaav commented Jun 24, 2016

Sorry! would you help me? i can't post with terms.

Term keyword = WPClient.GetTerm("category", 2);
var terms = new List<Term>();
terms.Add(keyword);
var post = new Post();
post.Title = "TEST";
post.Content = "THIS IS TEST";
post.PostType = "post";
post.Status = "publish";
post.PublishDateTime = DateTime.Now;
post.Terms = terms.ToArray();
string res = WPClient.NewPost(post);

but an error happen.

401
taxonomy in terms or terms_names is not supported by this post type.

any suggestion ?

@ghost
Copy link

ghost commented Aug 20, 2017

`
var client = new WordPressClient(new WordPressSiteConfig
{
BaseUrl = "http://localhost/wordpress",
Username = "123456",
Password = "123456",
BlogId = 1
});
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.GetTerm("category", 8) },
            //add tag and choice category ,category id need

           // You're wanted on this page: "/wp-admin/edit-tags.php, taxonomy=post_tag", hover over the categories, and see tag_id

            Status = "publish",
             
            
        };



        MessageBox.Show(client.NewPost(post));

`

I succeeded in creating a category, but I don't know how to make categories for new articles.

@abrudtkuhl
Copy link
Owner

@1428245421 #48 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants