0

I'm new to wordpress/php and I'm trying to create new posts dynamically through a xml file.

The xml I'm working with is this one.

The file get updated periodically during the day in a FILO fashion (new item are added at the top and the bottom one get push out) and right now we have someone manually creating new posts every few hours by grabbing a few value in each items.

So what I'm wondering is if I can automate this process?

Whenever this file is updated. Wordpress will create a new post with the new item only.

I'm able to pull values out of the xml file, but I'm having trouble creating a post dynamically and also set it so it will only create the new items, so I won't have any duplicates.

Also I'm not sure if this will effect much of the code, but I'm using the ACF plugin, so there are some custom fields in the post as well.

Thanks in advance.

0

Yes. From your description, it appears that all of the requirements to automate this process are in place.

The data is provided via XML which can be parsed with PHP to the necessary title, body, meta components. [search]

WordPress supports programmatic post insert/delete/edit functions so that part is fairly simple. [WP Codex]

Now you just need a PHP developer to write the code to your specs.

  • I would like to create the php code myself, do you have any recommendation on how to start outside of the two links in your post? – Radizzt Aug 23 '16 at 14:31
0

I figured it out on how to create post dynamically. I don't know if it's optimal, but it gets the job done.

I put all the necessary info in variables/arrays and added them into a for loop that does the insertion. It goes through the entire xml file, but will stop and break out of the loop if it comes to a post with the same title (I know this probably wont be the best unique identifier), this way it won't create multiple of the same post. once it break it tells the user how many post has been created:

$flag = true;
for($i=0; $i<24; $i++){
if(!get_page_by_title($b[$i]->title, OBJECT, post)){

    //check what category it's in.
    if(stristr($feedKeywordsArray[$i],"news")){
        $category = 3;
    }
    elseif(stristr($feedKeywordsArray[$i], "live")){
        $category =4;
    }
    elseif(stristr($feedKeywordsArray[$i], "insider")){
        $category =5;
    }
    elseif(stristr($feedKeywordsArray[$i], "local")){
        $category =6;
    }
    elseif(stristr($feedKeywordsArray[$i], "spotlight")){
        $category =7;
    }
    else{
        $category=1;
    }

    $my_post = array(
        'post_title'    => $b[$i]->title,
        'post_content'  => $b[$i]->description,
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array($category),
        'tags_input' => $feedKeywordsArray[$i]
    );

// Insert the post into the database.
    $post_id = wp_insert_post( $my_post );
    echo "<h1> Post: '" . $b[$i]->title . "' added</h1>";

    if($post_id>0){
        update_field('excerpt', (string)($b[$i]->description), $post_id);
        update_field('vamp-thumbnail', (string)($thumbnail[$i]), $post_id);
        update_field('vamp-video', str_replace("#autostart=on;", "#autostart=off;hidecompanion=on;autoadv=off;",(string)($a[$i][0])), $post_id);

    }
}
else{
    $flag = false;
    echo "<h2> Coming Out of the for loop after creating " . $i . " successful post(s).</h2>";
    break;
}
}

if($flag){
echo "Created all post";
}

?>

Your Answer

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

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