LoginSignup
4
4

More than 3 years have passed since last update.

PythonでWordPressに投稿、一覧取得

Posted at

python-wordpress-xmlrpc

xmlrpcでの認証等を簡単にしてくれるパッケージ
https://python-wordpress-xmlrpc.readthedocs.io/en/latest/overview.html

pip install python-wordpress-xmlrpc
# pip3 install python-wordpress-xmlrpc

記事の作成

記事を作成するサンプルコード

create_post.py
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo

wp = Client('http://sample.com/xmlrpc.php', 'username', '$password')
# WordPressのURL + /xmlrpc.php, ユーザー名, パスワードを指定


post = WordPressPost()
post.title = 'title'
post.content = 'content'

post.terms_names = {
'post_tag': '',
'category': ''
}

post.post_status = 'publish'
# post.date = datetime.now() - timedelta(hours=9) + timedelta(minutes=5+(5*delay))

wp.call(NewPost(post))
python create_post.py

記事の一覧

記事の一覧を出力するサンプルコード

list_posts.py
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost
from wordpress_xmlrpc.methods.users import GetUserInfo

wp = Client('http://sample.com/xmlrpc.php', 'username', '$password')

posts = wp.call(GetPosts({
                    'number': 50, 
                    'offset': 0, 
                    'orderby': 
                    'modified', 
                    'order': 'DESC', 
                    'post_type': 'post', 
                    'post_status': 'publish'
                }))
print(posts)

python list_posts.py
# [<WordPressPost: b'title'>, <WordPressPost: b'Hello world!'>, ...]

WordPressPostのオブジェクトについては以下を参照
https://python-wordpress-xmlrpc.readthedocs.io/en/latest/ref/wordpress.html#wordpresspost

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4