0

by a URL that generate an XML page, example: http://thegamesdb.net/api/GetGame.php?id=2 how can import it in Wordpress site with mapped request custom field? thanks Stew

1 Answer 1

2

Short answer: simplexml_load_file() + wp_insert_post()

You want to feed on the XML, and parse it for information that you can use to make posts. Here is a short example, because I am not going to dissect that particular feed for you:

$feed = simple_xml_load('http://example.com/feed.xml');

if($feed){
    foreach($feed['items'] as $item){
        $post_args = array(
          'post_title'    => $item->title,
          'post_content'  => $item->body,
          'post_status'   => 'publish',
          'post_author'   => 1, // this should represent you, or an author you want these to belong to
          'post_category' => array(8,39) // optional
        );
        $post_id = wp_insert_post($post_args);
        if($post_id){
            // add  meta values if you want
            $post_meta_values = array(
                'meta_key_slug_1'    => $item->some_information->,
                'meta_key_slug_2'    => $item->some_more_information,
            );

            // add meta
            foreach($post_meta_values as $key => $value) {
                update_post_meta($post_id, $key, $value);
            }
        }
    }
}

I would start by bringing in the xml file and then doing a print_r() of it so you can learn its structure.

4
  • i need add it the loop?
    – Stew
    Aug 29, 2013 at 14:30
  • No. This would be something you'd run as often as you need to sip on posts. Just once? Dunno. Another option to get the main feed object is fetch_feed() codex.wordpress.org/Function_Reference/fetch_feed. Basically, you're going to need to know how to work with XML objects, and use wp_insert_post() to actually create them. Where you put this code (could go into a function) and how you alter it, is entirely up to you.
    – GhostToast
    Aug 29, 2013 at 14:40
  • thanks for your infos but seem to be hard build it, im php newbie, know you a tutorial or plugin for do it?
    – Stew
    Aug 29, 2013 at 14:50
  • Yes. It is hard. But also empowering. No, I don't have a tutorial or anything. Start out small as a proof of concept, and work your way up. Good luck!
    – GhostToast
    Aug 29, 2013 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.