How to Display 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.
Create a counter of the WordPress posts that contain only the needed terms by using the SQL query built with the JetEngine plugin.
Primarily, let’s check the terms we will use for the query. Now we will go to the WordPress Dashboard > Pages > Categories directory.
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.
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.
Complete the Name field and select the “SQL Query” option as Query Type in the General Settings tab.
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.
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.
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.
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.
Proceed to WordPress Dashboard > Pages and open the page. You can do it by pressing the “Add New” button or editing the existing page.
Firstly, give a title to the page and click the “Edit with Elementor” button to proceed to the Elementor Editor.
Once the page is opened in the Elementor Page Builder, find the Counter widget in the search bar.
The primary setting we need is the Ending Number. Press the “Dynamic Tags” button and find the “Dynamic Function” item.
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.
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.
Also, mind changing the Title to the one appropriate for the case.
Once you add all the needed widgets and style up the page, hit the “Publish/Update” button to save the result.
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.
This time, there is only a count of the posts related precisely to the “interviews” term shown.
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.