How to export all wordpress post to an XML file with php?

0

I would like to know if anyone knows how to export all the posts from my site in wordpress through PHP code, which will take data such as the title of the post, prominent image, content, put it in its respective label and save it in an XML file in the server and that will be updated with each new post that will be created, in order of most recent date to the oldest I hope and someone can help me, because I am creating an application for android and I need the XML file as a source of data thanks.

    
asked by Jose Rodriguez 10.08.2016 в 06:30
source

3 answers

2

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).
    answered by 10.08.2016 в 17:51
    1

    What if you directly access the RSS that already exists and has XML format?

    For example:

    http://example.com/?feed=rss
    

    More information on link

        
    answered by 11.08.2016 в 22:43
    -1

    You can do it by installing the WP All Export plugin, you can download it here: link

    Once you have downloaded it unzip the file wp-all-export.zip and upload its contents to / wp-content / plugins /, then you go to the backoffice of your wordpress go to the plugins section and install it.

    I hope I have helped you.

        
    answered by 10.08.2016 в 07:22