1

Using WordPress 4.3 and we have a custom post type with custom fields. It's easy to just import all of this content from a DB to the WP DB and everything will be there. Problem is, all internal work is done on an external ERP server. No actual changes will be made to the WP site at all from an admin standpoint but

Right now we're using an ajax api to connect to our old website and pull in all the data. What I'd like to do is use this ajax api to pull in the data as the custom content type and save it to the WP DB. Then maybe have a cron or something that will check for changes between what is on the site and what is coming from the server.

The reason for this is think about this custom content type as a product. There are 70k products but are never sold from the website. The website just displays what is there. All sales, prices changes, etc are handled from the ERP server and then it needs to update the WP site.

I don't think it's a good idea to rebuild every time the user needs to look at something so I figured cron and changing the content that way would be the best bet.

1) We have the following on the old site:

Method: POST
URL: "http://api.example.com/api/product/search"
Headers: Content-Type = application/json
Body: {
  "categories": [],
  "brand": [],
  "sizes": [],
  "base": [],
  "location": [],
  "searchTerms": [""]
}

2) How would I use this to create populate the custom post type pages?

3) How would I use this to sort the results by the category?

4) I assume I'm going to be using custom taxonomy for the things like category, brand, size, etc so how do I map those and also fill the fields that are coming from the result? Those things are like price, description, etc.

5) Is there a method I can use to set up this sync as a cron so that it happens every night or something?

1 Answer 1

1

There's no need to do it via Ajax, you can use the curl libraries in WordPress to access data from remote sites. WordPress has a nice little interface via WP_HTTP_Curl for doing exactly that.

Creating posts from the data you retrieve could be done with wp_insert_post() and changes to posts would likely be done via wp_update_post()

You're desire to sort by category will need to be a bit more specific, WP automatically creates urls for categories at /category/<cat-slug> when pretty urls are enabled, there are also mechanisms for easily creating menus using those same category items.

WordPress also has Cron-like capabilities that can be supplemented with an actual cron job to ensure the tasks are run.

4
  • I will look into the parts you've mentioned. To specify my sort categories question, what I meant was that via ajax I was going to be returning all items and display on the page, but sort those items by category ie Cat1...item 1, item 2, item n; Cat2...item 1, item 2, item n; etc. What you mean is a category page for just those items from that category. Really we have one search results page and all categories on on that page and it divides the items.
    – o_O
    Sep 9, 2015 at 19:43
  • Depending on how many items you have that sounds like a rather bad UX for a search, but I obviously don't know the details. :) You can use some version/combination of get_posts, WP_Query, and/or pre_get_posts to control the output of your post queries.
    – totels
    Sep 10, 2015 at 7:32
  • Are there any docs you can mention that I can read up on these items? Going through Google isn't giving me anything other than rudimentary, "This exists" type posts and nothing so far with learning how to implement these ideas in practice.
    – o_O
    Sep 10, 2015 at 14:48
  • I made all of the functions in my answer links to the documentation for their related functions and such. For more in-depth tutorials you'll probably want to look through sites like Smashing Magazine or Tuts+
    – totels
    Sep 10, 2015 at 14:55

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.