Kevin
Kevin

Reputation: 21

How to submit custom fields data by python wordpress xmlrpc

I use wp job manager on my website. when I tried to add listing by xmlrpc, everything is fine, but Categories and Location are empty.

Screenshot Screenshot

My code is as below. Could you tell me what's wrong with my code? Thanks

from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts
from wordpress_xmlrpc.methods import posts
from wordpress_xmlrpc import WordPressTerm
from wordpress_xmlrpc.methods import taxonomies

wp = Client('http://127.0.0.1/15wp/xmlrpc.php', 'admin', '123456')

# now let's create a new product
widget = WordPressPost()
widget.post_type = 'job_listing'
widget.title = 'Widgetlast02'
widget.content = 'This is the widgets description.'
widget.post_status = 'publish'
widget.custom_fields = []
widget.custom_fields.append({
        'job_location': 'Newyork',
        'job_listing_category':  'hotel'
})
widget.id = wp.call(posts.NewPost(widget))

Upvotes: 1

Views: 1760

Answers (2)

Nikhil M Jain
Nikhil M Jain

Reputation: 41

The custom_fields attribute expects a list of dicts.

Within each dict, it expects values for key and value fields.

Here, key is the name of the custom field, and value is the value you want to assign to it.

Below is the snippet for your specific example.

widget.custom_fields = [
    {
        'key': 'job_location',
        'value':  'Newyork'
    }, 
    {
        'key': 'job_listing_category',
        'value':  'hotel'
    }
]

Upvotes: 1

Maurice
Maurice

Reputation: 13117

This is just a guess from looking at the documentation for WordPressPost in wordpress_xmlrpc:

(Emphasis mine)

class WordPressPost

Represents a post, page, or other registered custom post type in WordPress.

  • id
  • user
  • date (datetime)
  • date_modified (datetime)
  • slug
  • post_status
  • title
  • content
  • excerpt
  • link
  • comment_status
  • ping_status
  • terms (list of WordPressTerms)
  • terms_names (dict)
  • custom_fields (dict)
  • enclosure (dict)
  • password
  • post_format
  • thumbnail
  • sticky
  • post_type

It expects custom_fields to be a dict - you're creating a list and then inserting a dict into that list:

widget.custom_fields = []
widget.custom_fields.append({
        'job_location': 'Newyork',
        'job_listing_category':  'hotel'
})

This will probably work better:

widget.custom_fields = {
        'job_location': 'Newyork',
        'job_listing_category':  'hotel'
}

Upvotes: 0

Related Questions