Search code examples
rwordpressxml-rpc

Publish posts with custom post type from R via RWordpress and knit2wp


I am currently using the RWordpress-package from Duncan Temple Lang and the knitr-package from Yihui Xie to generate blog posts directly from R. For regular posts that works fine, however I would like to generate a post with a previously custom-made post-type. Via knit2wp i seem only to generate a regular new post, edit a post already published or generate a new page.

If I'd like to write a post by hand I would visit a page within the Wordpress-backend. For a regular post that would be

https://www.your-wordpress.blog/wp-admin/post-new.php

For the custom-made post that would be

https://www.your-wordpress.blog/wp-admin/post-new.php?post_type=custom

So my suggestion is that I have to send some additional information with the action-argument sent over the knit2wp-function of knitr.

The function-call of knit2wp is defined as follows:

knit2wp(input, title = "A post from knitr", ..., envir = parent.frame(), 
shortcode = FALSE, action = c("newPost", "editPost", "newPage"), postid, 
encoding = getOption("encoding"), publish = TRUE)

After defining the arguments sent to Wordpress via

  WPargs = list(content = list(description = content, title = title, 
                           ...), publish = publish)

the call itself is done:

  do.call("library", list(package = "RWordPress", character.only = TRUE))
  do.call(action, args = WPargs)

The information Wordpress provides hints me at a struct field called enclosure. My idea was thus to include a list named enclosure:

  WPargs = list(content = list(description = content, title = title, 
                           ...), enclosure = list(type = "custom"), publish = publish)

which leads unfortunately to an error message:

unused argument (enclosure = list(type = "custom", categories = c("test1", "test2"), wp_post_thumbnail = 12345))

I assume that I can include the post-type correctly if I modify some calls from the XMLRPC-package but I dont know where to start. Does someone has any idea how to generate custom types of posts via R in Wordpress?


Solution

  • Perhaps not direct an answer but I found a solution using curl-commands (cf Media Api Reference of WordPress). In that way I sent a command simply as a system call. I concatenate several strings up to a curl command, eg:

    header<- "--header 'Authorization: Basic your_token_here'"
    title<- "'title=Some title here'"
    excerpt<- "-d 'Some excerpt here'"
    url <- "-d 'slug=some-customized-url-structure-here'")
    command<-paste("curl ",header," -X POST -d ",title," -d 'status=publish' -d 'categories=12345' -d 'content= here goes the content' -d 'featured_media=xxxyyy' -d 'author=zzzz' ",url," ",excerpt,"  https://www.your-ur.l/wp-json/wp/v2/customized_structure_update",sep="")
    

    If I then fire up

    system(command)
    

    Everything works fine.