1

I am using python wordpress_xmlrpc library for extracting wordpress blog data. I want to fetch all posts of my wordpress blog. This is my code

client = Client(url, 'user', 'passw')
all_posts = client.call(GetPosts())

But this is returning only the latest 10 posts. Is there any way to get all the posts?

Naman Sogani
  • 943
  • 1
  • 8
  • 28
  • Haven't used the library, but the documentation says that you can pass a parameter indicating the number of posts you want: https://python-wordpress-xmlrpc.readthedocs.org/en/latest/ref/methods.html?highlight=get%20post#module-wordpress_xmlrpc.methods.posts – sgp Jun 02 '15 at 05:18
  • Yes, I tried that. Even if I pass any particular number, then also it returns exactly 10 posts. – Naman Sogani Jun 02 '15 at 05:30
  • How are you passing the `number` parameter? – sgp Jun 02 '15 at 05:31
  • client.call(GetPosts()) – Naman Sogani Jun 02 '15 at 05:34
  • The documentation says that you have to pass it as a `dict`. I'll write it down as an answer to clarify – sgp Jun 02 '15 at 05:37

2 Answers2

1

This is how I do it :

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


client = Client('site/xmlrpc.php', 'user', 'pass')
data = []
offset = 0
increment = 20
while True:
        wp_posts = client.call(posts.GetPosts({'number': increment, 'offset': offset}))
        if len(wp_posts) == 0:
                break  # no more posts returned
        for post in wp_posts:
                print(post.title)
                data.append(post.title)
        offset = offset + increment
Ahmed Soliman
  • 1,662
  • 1
  • 11
  • 16
0

According to the documentation, you can pass a parameter indicating how many posts you want to retrieve as:

client.call(GetPosts({'number': 100}))

Alternatively, if you want to get all the posts, check this out: https://python-wordpress-xmlrpc.readthedocs.org/en/latest/examples/posts.html#result-paging

Community
  • 1
  • 1
sgp
  • 1,738
  • 6
  • 17
  • 31
  • Thanks, and one more thing. Is there any way to know total number of posts. Because I dont know beforehand how many posts does a user have, and I want to fetch all posts of a particular user. – Naman Sogani Jun 02 '15 at 05:44
  • Try the result paging method mentioned in the answer – sgp Jun 02 '15 at 05:46