If you can get the job done with WordPress REST API, do it. Getting into XMLRPC is a whole other beast. For starters, XMLRPC requires you to connect using a private server. These days, you’ll need to encrypt that connection, too. I was building a tool that needed to search through my entire site’s post history, pull each post’s title and slug. I was convinced the WordPress REST API wouldn’t be able to do this for me, but I was wrong. I spent a bunch of time trying to set up XMLRPC just to find there’s a very simple search method built into the REST API.
https://example.com/wp-json/wp/v2/search?search=
Just enter your search query after the “search=” and you can now pull data on any blog posts that are resulted from this search. Because the result is JSON data, you can just store the data as JSON and start pulling the data you need.
search = requests.get('https://example.com/wp-json/wp/v2/search?search=tutorials')
results = json.loads(search.text) # Convert raw JSON to string, then back to JSON
Now we can see what posts we’re working with and pull data from them. Let’s get the post title and slug from some posts. Let’s just pull the data from the first result, which would be 0 in the index.
post_title = results[0]['title']
post_url = results[0]['url']
What if we wanted to store the title and URL of each post that came up in the search results? Let’s make 2 lists and append them as we go.
post_titles = []
post_urls = []
for i in results:
post_titles.append(i['title'])
post_urls.append(i['urls'])
Well, I got what I needed. Moving forward. Did this help?
WordPress REST API
Every WordPress site’s REST API can be accessed using the URL:
https://example.com/wp-json/wp/v2/
https://example.com/wp-json/wp/v2/posts
https://example.com/wp-json/wp/v2/search?search=