PRO: Search multiple Websites – Call several APIs to collect results

Your Challenge:

The User

  • fills out one Searchform,
  • sends the request and
  • gets the search results from several websites.

The JCI-PRO plugin can do that if the searched Servers have a JSON-Search-API. Usually, WordPress-Website does have those. See how this is done with JCI-PRO:


Step 1: Searchresult Page

Place this Shortcode onto a ordinary WordPress-Page:

[jsoncontentimporterpro nameoftemplate=searchresults orderofshortcodeeval=1]
  • Publish that page with e. g. URL “/search-jci-websites/”.
  • “orderofshortcodeeval” is available from Version 3.6.1 on. Before that: Set the Radio button at the JCI-PRO-Options in Tab “Shortcode-Settings” at “Usage of Shortcodes (JCI or other) in the JCI-twig-Template:” to “First option (default)…”

Step 2: Search result JCI-Template named “searchresults”

  • Name: searchresults
  • urlparam4twig : st#sno
  • URL of Template: dummyrequest (all API requests are done with the following Template-Text by twig and JCI)
  • Template-Text:
<style>input[type="number"] {   width:60px;}</style>

{% set sno = 10 %}{%if urlparam.sno %}{% set sno = urlparam.sno %}{% endif %}
{%if sno >100 %}{% set sno = 100 %}{% endif %}
{% set st = urlparam.st %}

<form action="/search-jci-websites/">
<input type="text" name="st" value="{{st | e}}" placeholder="Insert your search">
Show No of Hits (max. 100): <input type="number" name="sno" value="{{sno | e}}">
<input type="submit" value="Search">
</form>

{% macro displaySearchResult(resultArr) %}
{% set hits = (resultArr | length) %}
{% if hits  > 1 %}
<ol>{% for h in resultArr %}
{% if h.title %}
<li><a href="{{h.url}}" target="_blank" rel="noopener">{{h.title}}</a></li>
{% endif %}
{% endfor %}</ol>
{% else %}
<p>nothing found
{% endif %}
{% endmacro %}

{% macro doSearch(url, st, sno) %}
{% set secwp = url~st~ "&per_page="~ sno %}
{% set sc = '[jsoncontentimporterpro nameoftemplate=searchsingle url='~secwp~']' %}
{% set scout=(sc |doshortcode ) %}
{% set scoutJSON= scout| json_decode('TRUE') %}
{{ _self.displaySearchResult(scoutJSON ) }}
{% endmacro %}

{% set sarray = {
  'site1': {
    'website': 'www.json-content-importer.com',
    'url': 'https://www.json-content-importer.com',
  },
  'site2': {
    'website': 'api.json-content-importer.com',
    'url': 'https://api.json-content-importer.com',
  },
  'site3': {
    'website': 'doc.json-content-importer.com',
    'url': 'https://doc.json-content-importer.com',
  },
}
%}

{% for k,v in sarray  %}
</p><h2>{{v.website}}</h2>
{{ _self.doSearch(v.url~"/wp-json/wp/v2/search?search=", st, sno ) }}
{% endfor %}
  • Line 5: <form action="/search-jci-websites/"> Alter this depending on the URL you set for the Page in Step 1
  • Line 32ff: {% set sarray = { Alter this depending on the Websites you want to search.
  • Line 46: {{ _self.doSearch(v.url~"/wp-json/wp/v2/search?search=", st, sno ) }}
    Alter this if the JSON-APIs work in another way…

Step 3: Receiving JCI-Template named “searchsingle”

Create a new JCI-Template:

  • Name: searchsingle
  • Template-Text: {{ _context | json_encode }}
    This gives us the pure JSON from several Servers. If needed, you can alter the received JSON here.