I'm using MongoDB to store separate data for a WordPress website and need to be able to create pages with the data from MongoDB rather than from manually created pages in the dashboard. I've searched all over for this and can't seem to find anything for creating pages in the backend.

Is this even possible and if so, does somebody know a good starting point or way to do it?

Any information would be great.


You can programmatically create posts and pages using wp_insert_post or insert content using WXR files via WordPress's import feature.

To use wp_insert_post see the documentation here: http://codex.wordpress.org/Function_Reference/wp_insert_post

A simple example:

 $my_post = array(
          'post_title'    => 'hello',
          'post_content'  => 'This is my post.',
          'post_status'   => 'publish',
          'post_author'   => 1,
          'post_category' => array(1),
          'post_type'     => 'page'

          // Insert the post into the database
          wp_insert_post( $my_post );

BUT you are probably better off using WordPress's import functionality. WordPress uses WXR files which stands for WordPress Extended Rss ( if you know XML you will see it looks similar).

So it would be better for you to parse you mongo content into a WXR file and then just import it into WordPress.

A detailed write up on WXR: http://devtidbits.com/2011/03/16/the-wordpress-extended-rss-wxr-exportimport-xml-document-format-decoded-and-explained/

Also you can just add some dummy content to WP and export it and view the WXR file to see what format you should follow.

  • Awesome, this is exactly what I was looking for. Surprised I didn't find the wp_insert_post() function when searching for answers! Thanks for the WXR docs, I'm likely going to start with this to get the bulk of pages from mongo added. Though the insert_post is going to be handy to know. Cheers! – vikwer Dec 26 '13 at 6:07

Why would the type of DB you use have any relevance to the front end of the site? In theory, if you make the required SQL and DB access adaptations, you can run wordpress on MongoDB.

Pages are front end artifact used to manage the generation of content and url resolution and if you don't want to use them then why are you using wordpress at all?

Everything can be done in wordpress but it doesn't mean that wordpress is the best option for every task.

  • Wordpress is still being used on the front end by content editors etc., there's still lots of pages and posts being edited or added. It's a very nice CMS in some ways. My question was for a small feature I'm working on that takes advantage of the MongoDB server we use to store misc data. Small sidekick to the site. I agree if the font-end wasn't necessary then there would be better options than Wordpress. – vikwer Dec 26 '13 at 6:13
  • What I tried to say is that your question is like asking what color a car should be if we change the engine from V6 to V8. There is nothing special about mongoDB that in the context of wordpress makes it different from mysql, therefor there is no need to do anything special for wordpress to work with it except for api to access data. maybe you should ask specific question instead of asking a general one, try to give a real use case. – Mark Kaplun Dec 26 '13 at 7:29

First of all, it's my drawback that, I'm not accustomed to MangoDB, but I think I can answer the context.

To introduce your custom PHP into WordPress pages, a best way is to create a Page Template.

STEP I: Creating your custom template page
Create a new PHP page, totally new one with the following code:

Template Name: My Custom Page

Save the file inside your theme folder with a name you like, i.e. my-template.php.Then you can do whatever with PHP & MySQL inside the page template. But to work with MySQL in WordPress, there is a suggested way, and that is $wpdb.

So now for a check, just put the basic PHP thing: echo "Hello World!"; inside the page template.

STEP II: Using the template into page
In /wp-admin, create a new page, give it a title relevant to the PHP code or function of the page, and right to the editor, choose your template page from the drop down menu, you will find: "My Custom Page" there. Choose the template and save the page.

Now view the page and see how you are saying "Hello" to the World!


Another way is to use PHP within Pages, is to use a plugin like:

To work with the plugin, a tutorial is available here.

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.