22

I am creating a feature that creates automated posts in wordpress. Right now, the feature creates the wordpress blog post, but I can not enter the category.

    public class Post
    {

        public string Title { get; set; }

        public string Description { get; set; }

        public string PostType { get; set; }

        public DateTime DateCreated { get; set; }

        public DateTime DateCreatedGmt { get; set; }

        public List<string> Categories { get; set; }

        public List<string> MtKeywords { get; set; }

        public string MtExcerpt { get; set; }

        public string MtTextMore { get; set; }

        public string MtAllowComments { get; set; }

        public string MtAllowPings { get; set; }

        public string WpSlug { get; set; }

        public string WpPassord { get; set; }

        public string WpAuthorId { get; set; }

        public string WpAuthorDisplayName { get; set; }

        public string PostStatus { get; set; }

        public string WpPostFormat { get; set; }

        public bool Sticky { get; set; }

        public List<CustomFields> CustomFields;

        public Enclosure Enclosure;
    }

I tried to get to class first and pass only the category name to avoid errors:

        var wordpress  = XmlRpcProxyGen.Create<IWordpress>();

        Category[] categories= wordpress.getCategories(0, username, password);

The method that builds the post category receives as parameter. This method belongs to the class Post. The category is inserted in the post this way:

        Categories.Add(category.categoryName);

Could anyone help me? I've seen so many times this code that I can not see where I'm going wrong: (.

The attributes of Post were obtained in documentation : http://codex.wordpress.org/XML-RPC_MetaWeblog_API#metaWeblog.newPost. I'm using CookComputing.XmlRpc - http://xml-rpc.net/ - version 2.5.0

I realized after I posted the question was how wrong was handling category.

To place the post, we create:

class MetaWeblogClient : XmlRpcClientProtocol
{

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Post
    {
        public DateTime dateCreated;
        public string description;
        public string title;
        public string[] categories;
        public string permalink;
        public string postid;
        public string userid;
        public string wp_slug;

    }

And initialize the attributes in:

    public void newPost(string userid, string password, string description, string title)
    {
        this.Url = "http://#########.wordpress.com/xmlrpc.php";

        Post post = new Post();

        post.categories = new string[1];
        post.categories[0] = "Category Name";
        post.dateCreated = DateTime.Now;
        post.userid = userid;
        post.description = description;
        post.title = title;

        newPost("0", userid, password, post, true);

    }

    [XmlRpcMethod("metaWeblog.newPost")]

    public string newPost(string blogid, string authorId, string password, MetaWeblogClient.Post string description, bool publish)
    {
        //return string postid
        return returnPostId = (string)this.Invoke("newPost", new Object[] { blogid, authorId, password, description, publish });

    }

My mistake was not referring to initialize the array category. The structure above is not correct and I will remove it from my code.

Thank you for your attention.

svick
  • 236,525
  • 50
  • 385
  • 514
jAbreu
  • 307
  • 2
  • 10
  • 2
    Have you already tried it with a string array (`string[] categories`) instead of a list (`List<..>`)? – vstm Oct 15 '12 at 07:15
  • I realized after I posted the question was how wrong was handling category. I'll post how I could solve the problem. I'm ashamed for not having seen it before. You're right. Thanks for the answer :D – jAbreu Oct 15 '12 at 16:14
  • 1
    Hi @jAbreu - could you post your solution to the problem and mark it as the answer? Thanks! – doublesharp Oct 24 '12 at 13:49
  • The example code is invalid, works using correct category. – doublesharp Nov 16 '13 at 20:14

1 Answers1

1

Another thing you could use is the wp.newPost method:
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost

This uses 'taxonomies' instead of categories.

I'm currently updating the JoeBlogs Wordpress wrapper to support taxonomies (categories)
https://github.com/alexjamesbrown/joeblogs

Alex
  • 37,502
  • 51
  • 204
  • 332