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

How to add a category while posting to wordpres? #52

Closed
pumpadump opened this issue Sep 2, 2015 · 6 comments
Closed

How to add a category while posting to wordpres? #52

pumpadump opened this issue Sep 2, 2015 · 6 comments
Assignees
Labels

Comments

@pumpadump
Copy link

Hi, i tried to post this question to stackoverflow but the recommended tag "wordpress-java" does not exist.

I want to use the wordpressj api client to post to wordpress via xmlrpc.
I'm using it like this:

    Wordpress wp =  new Wordpress(username, password, xmlRpcUrl);
    Post post = new Post();
    post.setPost_title("test");
    DateTime date = new DateTime(1990, 12, 1, 1, 1);
    post.setPost_date(date.toDate());
    post.setPost_status("pending");
    post.setPost_excerpt("test test");
    wp.newPost(post);

How can I add a category to this post?

@canbican
Copy link
Owner

canbican commented Sep 2, 2015

You can find existing categories by:

List<Term> terms = WP.getTerms("category");

Each Term should have a name or slug to match for your required category. Find your term and do:

post.setTerm(Collections.singletonList(term));

to set one category. You can select multiple categories too.

BTW, I just noticed that creating a tag requires certain reputation in stackoverflow, which I don't have. I'll change the documentation until it matches my reputation on overestimation :)

@pumpadump
Copy link
Author

Hi thanks for the explanation I did as you suggested but I have a different Problem now, if i run this code:

Wordpress wp =  new Wordpress(username, password, xmlRpcUrl);   
Post post = new Post();
post.setPost_title("test");
DateTime date = new DateTime(1990, 12, 1, 1, 1);
post.setPost_date(date.toDate());
post.setPost_status("pending");
post.setPost_excerpt("test test");
List<Term> terms = wp.getTerms("category");
terms.removeIf(term -> !term.getName().equals("test"));
post.setTerms(terms);
wp.newPost(post);

i get the following exception:

Exception in thread "main" net.bican.wordpress.exceptions.InsufficientRightsException 
at net.bican.wordpress.Wordpress.newPost(Wordpress.java:1251) 
at test.main(test.java:39)

I'm testing this with and Administrator account on a wordpress.com hosted site.

I was able to post with categories to this blog before by pasing this hashmap to a xmlrpc client:

HashMap hmContent = new HashMap();
hmContent.put("title", "test");
hmContent.put("description", "test test");
hmContent.put("post_status", "pending");
hmContent.put("categories", new String[]{"test"});

I'm using jwordpress version 0.6.2

@canbican
Copy link
Owner

canbican commented Sep 3, 2015

Wordpress xml-rpc doc says it's one of the following:

  1. If the user does not have the edit_posts cap for this post type.
  2. If user does not have permission to create post of the specified post_status.
  3. If post_author is different than the user's ID and the user does not have the edit_others_posts cap for this post type.
  4. If sticky is passed and user does not have permission to make the post sticky, regardless if sticky is set to 0, 1, false or true.
  5. If a taxonomy in terms or terms_names is not supported by this post type.
  6. If terms or terms_names is set but user does not have assign_terms cap.
  7. If an ambiguous term name is used in terms_names.

But I always notice inconsistencies in documentation and error codes of the xml-rpc part of wordpress, should debug.

I don't currently have immediate access to a wordpress installation. I'll get back in a day.

@canbican canbican self-assigned this Sep 3, 2015
@pumpadump
Copy link
Author

It's a "mysite".wordpress.com Administrator account according to wordpress the Administrator Role encompasses the following capabilities:
https://codex.wordpress.org/Roles_and_Capabilities#Administrator

So i guess 1+2 are not relevant. 3 also because there is only one user on this site. 4 also because i don't pass sticky. 5 i don’t know, if I post via wp-admin I can assign categories to a pending post so i guess it is also not relevant. 7 should be also not relevant because this is the only term named "test" on this blog.
Leaves us with 6 but why should and Administrator Role not have this capability?

@canbican
Copy link
Owner

canbican commented Sep 4, 2015

This seems to be something I definitely overlooked, but hopefully fixed. I released a new version, please use 0.6.3. It should be visible on maven central. Meanwhile, you can use the jar file in the releases section.

Please let me know if it works for you.

@pumpadump
Copy link
Author

I can confirm that it works now. Good job thank you!

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

No branches or pull requests

2 participants