Stand with Ukraine. Fight for freedom and democracy

Donate
Back to contents

Displaying the Count of Posts Containing the Specific Term in Elementor

Discover how to show the count of posts containing the specific WordPress term using SQL query built with JetEngine’s Query Builder.

Tutorial requirements:

  • JetEngine plugin installed and activated;
  • Elementor Page Builder free version;
  • Terms (to the WordPress posts or CPT added).

Create a counter of the WordPress posts that contain only the needed terms by using the SQL query built with the JetEngine plugin.

Table of Contents:

Primarily, let’s check the terms we will use for the query. Now we will go to the WordPress Dashboard > Pages > Categories directory.

NOTE
Mind that you can try to recreate this use case with the custom categories.

Currently, we will use the “Interviews” and “Interviews 2021” terms. Remember that there are five and two items attached accordingly (in the Count tab), and look at their slugs.

wordpress posts categories

Query for Posts With the Specific Value in the Term

Head to WordPress Dashboard > JetEngine > Query Builder. Click on the “Add New” button to create a new query.

add new query in the query list

Complete the Name field and select the “SQL Query” option as Query Type in the General Settings tab.

general settings of sql query

Scroll down to the Custom SQL Query tab. Choose “terms” in the From table field and activate the Use Join switcher.

Press the “Add New” button to open the settings.

from table field in the custom sql query settings

Set the “Inner Join” Join Type or leave the field empty, as the needed type will be set automatically. The Join Table represents the database table that will be connected, so pick the “term_taxonomy” option.

In the When current table column and Is equal to base table column fields, choose the “term_id” options.

join tables settings

Move to the following Where (query clauses) tab and press the “Add New” button.

Select “terms.name” as the Column, “Like” as Compare operator, and complete the Value field with the term slug. The value should represent the term for which you want to show the count on the front end.

As “interviews-2021” also contain the “interviews” value, it will also be counted. Continue reading to know the exact way to count all their posts or see how to include only “interviews” in the second part of the article.

where query clauses compare like operator

Now scroll down to the Include columns field and complete it with the “term_taxonomy.count” value.

Finally, you can press the “Add Query” button to finish the initial setup.

include columns field in the sql query settings

Proceed to WordPress Dashboard > Pages and open the page. You can do it by pressing the “Add New” button or editing the existing page.

wordpress pages directory

Firstly, give a title to the page and click the “Edit with Elementor” button to proceed to the Elementor Editor.

terms query page in the block editor

Once the page is opened in the Elementor Page Builder, find the Counter widget in the search bar.

counter widget in elementor

The primary setting we need is the Ending Number. Press the “Dynamic Tags” button and find the “Dynamic Function” item.

ending number field dynamic tags options

Make sure you have set up the available settings. Choose the “SQL query result” Function and find the “term_taxonomy.count” in the Query Column drop-down list.

You can now see that the number on the counter has changed to “5”. This amount of posts we have attached to the “interviews” category.

term taxonomy count query column

If you move to the Additional Function and select the “Sum” option, you will see that the counter number has been changed to “7”.

So now the counter also includes the “interviews-2021” posts.

additional action field in the counter widget settings

Also, mind changing the Title to the one appropriate for the case.

title field in the counter widget settings

Once you add all the needed widgets and style up the page, hit the “Publish/Update” button to save the result.

two terms counter

Counter of One Particular Term

Additionally, you can follow the next steps to get results only for one specific term.

Return to Query Builder and find the query you have built before. 

Open it and scroll down to the Join Tables tab. Here you can delete the set clause and create a similar one, but this time complete the Compare field with the “Equal (=)” value.

Now press the “Add/Update Query” button and head to the front end to check the result.

So note that there is no need to set the Additional Function in the Counter widget’s Ending Number field.

where query clauses tab

This time, there is only a count of the posts related precisely to the “interviews” term shown.

counter result on the front end

That’s it; you have learned how to display the count of the posts containing particular needed WordPress terms using JetEngine’s Query Builder functionality.