Another option is to use the JSON API of WordPress, getting the posts with the HTTP API of WordPress, and then convert the obtained array into XML.
You have to use the plugin that enables the endpoint JSON .
The wp_remote_get function of the WordPress HTTP API allows you to make a query, saving the complete answer (including headers) in a variable.
$response = wp_remote_get('http://local.wp.dev/wp-json/wp/v2/posts/');
Tip: you can add? _embed to include tags, categories and other data, embedded in each post, and not just the related ID.
The JSON that represents the posts (the R in REST), you recover it with:
$json = wp_remote_retrieve_body($response);
You convert the JSON into an array with json_decode.
$array = json_decode( $json, $assoc = true );
Convert the array you obtained in XML, depending on the version of PHP you are working with, you can choose a modern library or your own classes.
If you already work with PHP 5.6 you can use FluidXML, which is related to a question similar to this one (convert array to XML) in OS: link
If you are still working with PHP 5.2, the following answer gives you a class with which you can work to solve the problem
You also have to include the highlighted image (thumbnail) in the JSON, because WordPress does not include it by default, I hope this code will work for me that I am using for this same purpose (although I do not need the conversion to XML), you have to include in a specific plugin of your site, or in the functions.php of the active theme:
add_action('rest_api_init', 'register_thumbnail_field_for_posts');
function register_thumbnail_field_for_posts()
{
register_rest_field('post', 'thumbnail', [
'get_callback' => 'get_thumbnail_of_post',
'update_callback' => null,
'schema' => [
'description' => 'Imagen destacada del post',
'type' => 'string',
'format' => 'url',
'context' => ['view'],
'readonly' => true,
]
]);
}
function get_thumbnail_of_post($object, $field_name, $request)
{
$attachment_id = get_post_meta($object['id'], '_thumbnail_id', true);
if (!$attachment_id) {
return;
}
$url = wp_get_attachment_url($attachment_id);
return $url;
}
Notes and other references (I can not put more links).
- Search wordpress.org for WordPress REST API (Version 2) (do not let me share the link here).