PRO: Generate Custom Post, Custom Post Fields with Twig and JSON and use it with Elementor

In short: Create “Custom Post Pages” out of “Custom Post Types” and fill it with JSON-data. Then use the Pafges with dynamic Elementor fields.

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 plugin we create a CPT with slug (=”Singular Label”) “clientzone”.
And with Advanced Custom Fields (ACF) we add the Custom Post Fields (CPF) with slug “id” and “categoryname” as simple text.

The result of this: You should have a CPT “clientzone” in the menu. If you add one you can add content and the CPF. For further details see the manual of the two plugins “Custom Post Type UI” and ACF used (this step is completely without JCI). “Pods” and “Toolset” are two other Plugins also very good for creating CPT.

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

Create a new JCI-template:
URL: The JSON we use in this example is https://api.json-content-importer.com/extra/json/clientzone.json
Name of template: “generate”
twig-template:

{% set cpt_slug = 'clientzone' %}
{% set cpt_cpfkey = "cptidentifier" %} 
{% set cpt_cpfval = "uniqueidentifierforallpages" %}
{% set list_of_pageIDs_out_of_json = [] %}

{% for f in items | slice(0,3) %}
    {% if f.id %} 
        {% set fid = f.id %}
        {% set fcategoryname = f.category_name %}
        id: {{fid}} - category_name: {{fcategoryname}}<br>
        {% set title = fid ~ ' ' ~ fcategoryname %}
        {% set pageslug = fid ~ '-' ~ fcategoryname %}
        {% set excerpt = "this-is-excerpt" %}
        {% set publishdate = ("now" | date("Y.m.d")) %}
        {% set postStatusUsed = "publish" %}
        {% set authorid = "" %}
        {% set debug = FALSE %}
        {% set updatehash =  'up' ~ ("now" | date("dmYHis")) %} {# always updating.... #}
        {% set content = "Dummytext: " ~ updatehash%}

        {% set checkidentifier = wp_get_cp_by_cpf_keyvalue(cpt_slug, "identifier", pageslug) %}
        {% if checkidentifier %}        
            {# UPDATE #}
            {% set pageid = checkidentifier.0 %}
            {% set storedupdatehash = wp_get_custom_field_value(pageid, "updatehash") %}
            {% if storedupdatehash.0==updatehash %}  
                no update needed for page {{pageid}}<br>
            {% else %} 
                {% set update = wp_update_custom_post(pageid, title, pageslug, content, excerpt, publishdate, postStatusUsed, authorid, debug) %}
                {% set updateinfo = wp_insert_custom_field_keyvalue(pageid, "updatehash", updatehash) %}
                do update for page {{pageid}}<br>
            {% endif %}            
        {% else %} 
            {# new #}
            {% set pageid = wp_new_custom_post(cpt_slug, title, pageslug, content, excerpt, publishdate, postStatusUsed, authorid, debug) %}
            {% set cpf = wp_insert_custom_field_keyvalue(pageid, cpt_cpfkey, cpt_cpfval) %}
            {% set cpfidenbtifier = wp_insert_custom_field_keyvalue(pageid, "identifier", pageslug) %}
            {% set updateinfo = wp_insert_custom_field_keyvalue(pageid, "updatehash", updatehash) %}      
            new page: {{pageid}}<br>
        {% endif %}
        {% set list_of_pageIDs_out_of_json = list_of_pageIDs_out_of_json | merge([pageid]) %}
        {% set newcpf = wp_insert_custom_field_keyvalue(pageid, 'id', fid) %}
        {% set newcpf = wp_insert_custom_field_keyvalue(pageid, 'categoryname', fcategoryname) %}
        <hr>
    {% endif %}
{% endfor %}

{% set getallcreatedpages = wp_get_cp_by_cpf_keyvalue(cpt_slug, cpt_cpfkey, cpt_cpfval) %}
{% for existingPage in getallcreatedpages %}
    {% if existingPage in list_of_pageIDs_out_of_json %}  
        {# existingPage is in JSON #}
    {% else %} 
        {# existingPage is NOT in JSON #}
        delete {{existingPage}}<br>
        {% set delflag = wp_delete_custom_post(existingPage) %}
    {% endif %}
{% endfor %}

The above code is doing this:

  • Set the parameter for the CPT (and a unique “uniqueidentifierforallpages” – with this we identify all generated pages).
  • Loop through JSON and create or update the pages incl. adding CPF.
  • Check and delete previously created pages, which are not in the current JSON any more.

Remarks:

  • If you use a CPT created by Toolset add “wpcf-” to the CPF-slug

Step 3: Use the JCI-Template and create the Custom Pages

Then you can use the following Shortcode on a WP-Page:

[[jsoncontentimporterpro nameoftemplate="generate"]]

When executing this Shortcode you should get 3 Custom Posts in the Menu for “clientzone”.
Executing again tells you, that all 3 pages are already there.
You can run auto-updates with the Plugin GetURLCron.

Step 4: Generated Pages and Elementor

With Elementor you can design the generated Pages using the CPF unsing dynamic fields in Elementor.