# Display Data Explorer query results in WordPress (with TwigAnything)

**URL:** <https://meta.discourse.org/t/display-data-explorer-query-results-in-wordpress-with-twiganything/40956>\
**Category:** Administrators\
**Tags:** data-explorer, how-to\
**Created:** [March 13, 2016, 6:43am UTC](https://meta.discourse.org/t/display-data-explorer-query-results-in-wordpress-with-twiganything/40956 "2016-03-13T06:43:07Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)\
**Post date:** [March 13, 2016, 6:43am UTC](https://meta.discourse.org/t/display-data-explorer-query-results-in-wordpress-with-twiganything/40956/1 "2016-03-13T06:43:07Z")

</div>

[Data Explorer](https://meta.discourse.org/t/data-explorer-plugin/32566) plugin lets us extract and explore about any data from Discourse.

In this tutorial I’ll show you how to **display it in WordPress** , like this:

 ![](https://global.discourse-cdn.com/meta/original/3X/2/1/21c6e0fe552d6fe327f62e9efed9f8ef15c895cf.png)

- The data can be displayed in posts/pages, widgets or in Visual Composer blocks
- The data is optionally cached in WordPress
- You have full control on how data is output.
- In this tutorial we will use [Twig Anything](https://meta.discourse.org/t/announcing-wordpress-plugin-to-display-any-data-from-discourse/30968) WordPress plugin (I’m the plugin author)

# Use-case

So, let’s build a list of topics from particular categories published in the last 7 days that had comments, sorted by popularity.

We first build a query in [Data Explorer](https://meta.discourse.org/t/32566?silent=true) to retrieve the topics list from database by our criteria.

Once done, we then use Twig Anything to fetch this data from Discourse and output it in WordPress.

_This tutorial is built based on the live Discourse installation at [forum.kozovod.com](http://forum.kozovod.com), a Ukrainian community of goat farmers, which I manage._

# Step 1. Build [Data Explorer](https://meta.discourse.org/t/32566?silent=true) Query

In our query, we will need:

- topic score, which we display in the list
- topic `id` and `slug`, which we use to build a link to the topic
- topic title, which makes up the text of the link
- username of the user who created the topic, to show it in the list

Here is our query

```sql
SELECT score, t.id, t.slug, t.title, u.username
FROM topics t
LEFT JOIN categories c ON c.id = t.category_id
LEFT JOIN users u ON u.id = t.user_id
WHERE c.name IN (
    'Козоводство',
    'Разведение',
    'Содержание и кормление',
    'Ветеринария',
    'Селекция и генетика')
  AND t.created_at >= current_timestamp - interval '7 days'
  AND t.posts_count >= 2
  AND t.visible = true
  AND t.closed = false
  AND t.archived = false
  AND t.pinned_globally = false
  AND t.archetype = 'regular'
  
ORDER BY score DESC
LIMIT 10

```

Here is how to adjust the query to your needs:

- `LIMIT 10` - change to the maximum number of topics you want in your list
- `AND t.posts_count >= 2` - change to the minimum number of posts in the topic to be qualified for the list
- `interval '7 days'` - change to how many days are OK for the topic creation date to let it appear in the list
- `c.name IN (...)` - list category names to only allow topics from those categories; remove the clause altogether if you don’t want to filter by categories

When running the query in the [Data Explorer](https://meta.discourse.org/t/32566?silent=true), you’ll see an output like on the following screenshot. Please note the columns are indexed from 0 - 4, we’ll need this later:

 ![](https://global.discourse-cdn.com/meta/original/3X/1/7/175b9c561edec823759d884b149e045a1cc99457.png)

You’ll also need query ID, which you can find from the URL after saving and running your query:

`http://forum.kozovod.com/admin/plugins/explorer?id=15`

Here, the `id` is `15`.

# Step 2. Create Twig Template in WordPress

In WordPress, start by creating a new Twig Template:

 ![](https://global.discourse-cdn.com/meta/original/3X/2/1/217bb4c43025daa81fd34070ac5b90ea1fb6334d.png)

Give it a meaningful name:

 ![](https://global.discourse-cdn.com/meta/original/3X/4/5/45e4f3377bf7fc0811bf5e9f2c31a806e8444954.png)

To not get confused when the number of Twig Templates in my WordPress grows, in the name I use both **`Kozovod`** , which is my Discourse instance name, and **`15`** , which is my [Data Explorer](https://meta.discourse.org/t/32566?silent=true) query’s ID.

Next, scroll down and find the “Data Source” box and configure it as follows:

 ![](https://global.discourse-cdn.com/meta/original/3X/6/c/6c483423ea64bf84485c5fe238f27df0c653d1fa.png)

- Source Type: **`URL`**

- Data Format: **`JSON-encoded string`**

- Cache lifetime in seconds: leave empty for now while we’re setting it up

- Data errors handling: choose what WordPress should show if an error occurs while retrieving or rendering data from Discourse

- Data URL: here, you’ll have to build the correct URL: **`http://<your.discourse.com>/admin/plugins/explorer/queries/<ID>/run?api_username=...&api_key=...`**

- HTTP Request Method: Select **`POST`**. It won’t work with `GET` to fetch data from [Data Explorer](https://meta.discourse.org/t/32566?silent=true).

Now, enter the following template that will render your data in a list:

```twig
<ul>
{% for row in data.rows %}

  <li>
    <a href="http://forum.kozovod.com/t/{{row[2]}}/{{row[1]}}" style="color: blue">
      {{row[3]}}
    </a>
    by <code><strong>{{ row[4] }}</strong></code> scored
    <strong>{{ row[0]|number_format(1) }}</strong>
  </li>

{% endfor %}
</ul>

```

This template is in [Twig Syntax](http://twig.sensiolabs.org/), which is very easy to learn.

Note how we loop over `data.rows`  
The `data` is how you access the JSON data fetched from Discourse.  
The `data.rows` contains to the list of rows as you saw them in [Data Explorer](https://meta.discourse.org/t/32566?silent=true), in the same sequence.

Now, to access a particular column we use **`{{ row[0] }}`**, where 0, 1, 2 etc is the index of the column.

So, for example, in this line we use column No. 4 to output the nickname of the user who created the topic:

```twig
by <code><strong>{{ row[4] }}</strong></code> scored

```

We also use topic ID and topic slug to build a URL that links to that topic:

```twig
href="http://forum.kozovod.com/t/{{row[2]}}/{{row[1]}}"

```

Which will eventually become something like this:

`http://forum.kozovod.com/t/istochniki-sovremennyh-znanij-po-genetike/2770`

Finally, we use [Twig’s `number_format` filter](http://twig.sensiolabs.org/doc/filters/number_format.html) to format topic scores so that only one digit after decimal point is shown:

```twig
<strong>{{ row[0]|number_format(1) }}</strong>

```

# Step 3. Preview your Twig Template

All done! Now click on the Preview button in WordPress:

 ![](https://global.discourse-cdn.com/meta/original/3X/3/e/3ed3ce7a0046eabee3909af83d39c97a3206f413.png)

A new tab will be opened displaying the template preview:

 ![](https://global.discourse-cdn.com/meta/original/3X/d/1/d1f4e944dc701ddbca34c6881149bdf7b4ce5a0b.png)

If you’re satisfied with the output, click Publish to make the template available for embedding and proceed to the next step!

 ![](https://global.discourse-cdn.com/meta/original/3X/7/c/7c6eb29a5b7545b9837e8ac11b68917b75558464.png)

# Step 4. Embed your data anywhere in WordPress

Showtime!

Now copy the shortcode to clipboard and paste it into your post or page:

 ![](https://global.discourse-cdn.com/meta/original/3X/e/b/ebf8f93320dd8d5eb72290cbe98d58afa1d7c0e3.png)

Alternatively, you can use a Twig Template Widget:

 ![](https://global.discourse-cdn.com/meta/original/3X/1/6/165e1fc0aeef3e55a4c0f4fed8576940fcd0afdc.png)

Finally, you can embed it right in Visual Composer:

 ![](https://global.discourse-cdn.com/meta/original/3X/c/d/cd651f5c72986474c3a2a19cd4a2cf662c67157f.png)

* * *

When [Data Explorer](https://meta.discourse.org/t/32566?silent=true) and Twig Anything work together, endless possibilities come for integrating Discourse and WordPress.

Twig Anything is not free, you can [buy it here](https://twiganything.com/download/). It’s not expensive either, and your purchase will help me to dedicate time and develop more features and add-ons, which are always free.

Enjoy and ask me any questions!

---

_[View the full topic](https://meta.discourse.org/t/display-data-explorer-query-results-in-wordpress-with-twiganything/40956)._
