For the purpose of this question, I have two WordPress websites. Let's call them "Portfolio" and "News". Portfolio is a "pages" only website while "News" is a "posts" only website. On the Portfolio website, I need to display the top X posts from the News website. Both websites are on the same server, with different MySql databases.

I tried using the JetPack "Display WordPress Posts" widget, but the way XML-RPC works with that widget is not conducive to my environment. So, I want to write my own "widget" to do this work.

Keep in mind that I have never coded a widget for WordPress and so far I have a basic-intermediate level understanding of PHP. That is, more than theory, I could use some examples.

I came across http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts and looks like it is something I could use. However I found no examples on how I could use this API to pull content from the blog.

Thank you.

  • 1
    Define "top X"? Latest? It might be easiest to just use RSS widget with feed from that site. – Rarst Oct 23 '14 at 9:46

I spent the better part of my holiday today to re-wire the JetPack Display WordPress Post widget so that it now works for me and resolves my issue. Documenting verbosely so that it could help someone else with similar issues.

(I have posted the fix on my blog: [DEEP LINK WARNING!] http://blog.sujay.sarma.in/wp-content/uploads/wordpress-post-widget.zip )

Basically, I found that the widget does not wait long enough during the wp_remote_get call. It times out after 5 seconds. I added a timeout of 30 seconds to this call. This resolved the main issue of not getting the posts. [RESOLVED why I wanted to use XML-RPC calls to do this]

Once that was done, I discovered a new "bug" where the error returned if there are transient failures in the API call are also cached. Cleaned up those as well, ensuring only successful post pulls are cached. Optimized a step further by caching the parsed JSON instead of the original string. Finally, I did away with the call to retrieve the site's Id (wasted roundtrip, bandwidth saved at both ends) as the API call works just as fine with the DNS name instead of the Site ID.

The Rest API hosted at wordpress.com seems to have a performance issue on its own as it intermittently fails roughly 50% of the time. These failures have the error message that your site's DNS name could not be resolved. This actually caused WordPress Support ("happiness engineers") to spend an entire day trying to convince me that my DNS was set up all wrong.

(url: https://public-api.wordpress.com/rest/v1/sites/[site])

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy