Using XMLRPC to get a List of Posts from WordPress

Today I’m going to demonstrate how one can go about remotely fetching a list of posts for any post type including custom post types remotely using XML-RPC in WordPress. What I thought was going to be a simple task ended up taking me the majority of an entire day to work out yesterday, so I am posting my solution here for future reference and to help others solve the same problem.

So during my quest to figure out a solution for listing posts for a remote WordPress blog inside the wp-admin of another WordPress blog, I came across a couple promising functions such as wp.getPosts, blogger_getRecentPosts and similar existing XML-RPC functions that reside within wp-includes/class-IXR.php. None of those did the trick however, so below is the final solution I came up with which involves extending the XML-RPC class noted.

Extending the XML-RPC functions in WordPress

Here’s what you can add to your theme’s functions.php or plugin file to extend the class-IXR.php class to allow remote listing of post titles for any given registered post type:

function wp_getTits() {
// Get the ‘Profiles’ post type
/*$args = array(
    ‘post_type’ => ‘oils-blends’,
);*/
$args = array(
‘nopaging’ => true,
‘numberposts’    => 300,//increase if more than 300 products exist!
‘post_type’        => ‘oils-blends’,
‘post_status’   => ‘publish’//only exporting published products!
);
$loop = new WP_Query($args);
$tits = “”;
while($loop->have_posts()): $loop->the_post();

$titis = get_the_title();
$tits .= $titis.”zxz”;

endwhile;
wp_reset_query();
return $tits;
}

function wp_new_xmlrpc_methods( $methods ) {
    $methods[‘wp.getTits’] = ‘wp_getTits’;
    return $methods;   
}
add_filter( ‘xmlrpc_methods’, ‘wp_new_xmlrpc_methods’);

After you add the above code to functions.php or your plugin file, then you can use the following block of code within a plugin file of another WordPress site to remotely list the posts of a WordPress blog on another server. I added the following code inside my plugin which was used to post to a remote WordPress site. I needed to read the existing posts in order to know whether to write or update the posts that I was pushing to the second blog:

<?php
echo “<p>Starting to get posts from remote website…</p><h3>List of Titles:</h3>”;

$rpcurl = “http://example.com.au/wordpress_dir/xmlrpc.php”;

$request = xmlrpc_encode_request(‘wp.getTits’,’1′);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
//echo “Curl results: $results<hr />”;
curl_close($ch);
//separate titles by zxz string:
$titleArray = explode(“zxz”,$results);
    foreach($titleArray as $curTit){
        echo “$curTit<br />”;
    }//END FOREACH TITLE
echo “<p>Finished…</p>”;

?>

That’s all folks. Now you can remotely fetch posts from one WordPress site to another without any prior knowledge of post IDs etc. Now if you do know the post IDs, then you could use the existing XML-RPC method, wp.getPost which uses the post ID as an argument. I’ll share this code with you too even though it wasn’t the final solution for me, it could work for someone that happens to know the post IDs for which they need to grab remotely. The following code was tested and working as of WordPress version 4.2.3:

Listing Posts Remotely using wp.getPost

As you will see, it is a lot easier to get post data when you know the post ID. Here is all you have to add to your WordPress plugin file to remotely grab a post using wp.getPost when you know the post’s ID:

<?php
echo “<p>Starting to get post data from remote site…</p>”;

$rpcurl = “http://example.com.au/wordpress_dir/xmlrpc.php”;
$username = ‘admin’;
$password = ‘password’;
$post_id = 2820;
include(‘../wp-includes/class-IXR.php’);  
        $client = new IXR_Client($rpcurl);
     
        $USER = $username;
        $PASS = $password;
               
        if (!$client->query(‘metaWeblog.getPost’,$post_id, $USER,$PASS))
        {  
            echo(‘Error occurred during getting post.’ . $client->getErrorCode().”:”.$client->getErrorMessage());  
        }
        $post = $client->getResponse();
       
        var_dump($post);
echo “<hr /><p>Finished….</p>”;
?>

Of course you have to edit the URL, username and password above, but after that it will work fine.

Summary

So I’ve given you two ways to get post data from WordPress sites remotely. If you know the post IDs, then use the last method which is simpler, otherwise you have to extend the class explained above using the first two code snippets on this page. Good luck!

One Reply to “Using XMLRPC to get a List of Posts from WordPress”

Leave a Reply

Your email address will not be published. Required fields are marked *