PRO: Generate Custom Post, Custom Post Fields, Taxonomies with Twig and JSON

Howto create and update Custom Posts out of JSON

Besides putting together a Shortcode for creating Custom Post Pages out of Custom Post Types out of JSON you can do that in a different way: The big advantage is the easy way to update Custom Posts, the disadvantage is that it’s a “programming way” to do that.

Step 1: Create a Custom Post Type (=CPT)

You can create CPTs in many ways. In this example we use the free Plugin “Custom Post Type UI“. With that pluign we create a CPT with slug “cptexample”., which is connected to the Taxonomy with slug “cptaxonomyexample”. And with Advanced Custom Fields we add the Custom Post Fields (CPF) with slug “cpf1” and “cpf2”.
The result of this: You should have a CPT “cptexample” in the menu. If you add one you can add content, CPF and a Taxonomy. For further details see the manual of the two plugins used (this step is completely without JCI).

Step 2: Create a JCI-Template which is using the JSON to create Custom Pages, CPF and entries in the Taxonomy

The JSON we use in this example is http://api.json-content-importer.com/extra/json/meetup/1.json, a list of several events. Create a new JCI-template names “cptwig-create”, with the above URL and this twig:
{% set slug = 'cptexample' %}
{% set taxonomyslug = 'cptaxonomyexample' %}
{% for i in _context %}
{% if i.id %}
{% set cpfound = wp_get_cp_by_cpf_keyvalue(slug , 'cpf1', i.id) %}
{% if cpfound.0 %}
{{i.id}} already existing, pageid: {{cpfound.0}}<br>
{% else %}
name: {{i.name}}<br>
{% set titel = 'titel'~i.name %}
{% set pageslug=  'name'~i.name %}
{% set content = 'content'~i.name %}
{% set publishdate = "21.04.2021" %}
{% set postStatusUsed= 'publish' %}
{% set newpageid = wp_create_new_custom_post(slug, titel, pageslug, content, publishdate, postStatusUsed, 1, TRUE) %} 
newpageid : {{newpageid }}<br>
{% set  rescpf = wp_insert_custom_field_keyvalue(newpageid , 'cpf1', i.id, FALSE) %}
{% set  rescpf = wp_insert_custom_field_keyvalue(newpageid , 'cpf2','cpf1'~i.name, FALSE) %}
{% set taxonomyValue = 'tax-'~i.name %}
{% set tax = wp_insert_taxonomy(newpageid, taxonomyslug, taxonomyValue, TRUE) %}
<hr>
{% endif %}
{% endif %}
{% endfor %}
Remarks:
  • If you use a CPT created by Toolset add “wpcf-” to the CPF-slug
Then you can use the following Shortcode on a WP-Page:
[jsoncontentimporterpro nameoftemplate="cptwig-create"]
When executing this Shortcode you should get 8 Custom Posts in the Menu for “cptexample”.
Executing again tells you, that all 8 pages are already there.
The code in detail:
The Code loops through the JSON (“{% for i in _context %}”) and checks, if the field “id” is set (“{% if i.id %}”).
Then we store the slug-name of the Custom Post Type (“{% set slug = ‘cptexample’ %}”), this is the blueprint for the Custom Posts.
“{% set cpfound = wp_get_cp_by_cpf_keyvalue(slug , ‘cpf1’, i.id) %}” checks, if there are Custom Posts which have in the CPF “cpf1” the vlaue for “id”. If so (“{% if cpfound.0 %}”), the page is already there and nothing is changed. So the CPF “cpf1” and it’s content is the identifier for a JSON-item.

If there is no such Custom Post, we create one:
Together with the JSON-data we set several variables “titel”, “pageslug”, “content”, “publishdate”, “postStatusUsed”: These are the pieces a Custom Post is made of.
“{% set newpageid = wp_create_new_custom_post(slug, titel, pageslug, content, publishdate, postStatusUsed, 1, TRUE) %}” creates the Custom Post (TRUE menas that sdome debug info are displayed, you might delete this if not needed any more).
With “newpageid” we have the wordpress-pageid of the new Custom Post, which is the unique identifier for a page.

“{% set rescpf = wp_insert_custom_field_keyvalue(newpageid , ‘cpf1’, i.id, FALSE) %} {% set rescpf = wp_insert_custom_field_keyvalue(newpageid , ‘cpf2’,’cpf1’~i.name, FALSE) %}” fills the two CPF “cpf1” and “cpf2”. Where “Cpf1” gets the “id” to have a unique identifier.

Open one of the generated Custom Pots: There should be content, two filled and a Taxonomy-Value.

Step 3: Update Custom Posts

For updating a Custom Post we must know, what Custom Post belongs to what JSON item. By having a connecting key this is possible: In the example this is the CPF “cpf1” with the “id” from the JSON. So updating means here: Looping through all JSON-items and search for the corresponding Custom Post identified by the value of “cpf1”. Then we update the Custom Post Fields of the so found Custom Post. For that create another JCI-Template with URL http://api.json-content-importer.com/extra/json/meetup/1.json, name “cptwig-update” and this twig-code:
{% set slug = 'cptexample' %}
{% set taxonomyslug = 'cptaxonomyexample' %}
{% set cleartax = wp_clear_taxonomy(taxonomyslug, FALSE) %}
{% for i in _context %}
{% if i.id %}
name: {{i.name}}<br>
{% set cpfound = wp_get_cp_by_cpf_keyvalue(slug , 'cpf1', i.id)  %}
{% if cpfound.0 %}
{% set tz = date().format('U') %}
{% set rescpf = wp_insert_custom_field_keyvalue(cpfound.0, 'cpf2', tz ~ ' - '~i.name, FALSE) %}
{% set taxval= 'tax-'~cpfound.0 %}
{% set addtax = wp_insert_taxonomy(cpfound.0, taxonomyslug, taxval, TRUE) %}
{% endif %}
{% endif %}
{% endfor %}
Then you can use the following Shortcode on a WP-Page to update the cpf2 and the Taxonomy:
[jsoncontentimporterpro nameoftemplate="cptwig-update"]