How to Conditionally Hide the Query Total Block When No Results Are Found
Content
A common challenge when using the Query Loop block is managing the Query Total block, which displays the number of posts found. While useful, it can be visually jarring when a query returns zero results, as it displays "0 results found." Many users want this block to hide automatically in such cases, but the block itself lacks a built-in setting for this behavior.
Why This Happens
The Query Total block is a simple counter. It does not have conditional logic to evaluate whether posts were actually found in the associated Query Loop. Furthermore, it does not output any specific CSS class based on the post count, which makes it impossible to hide using CSS alone.
How to Fix It: A JavaScript Workaround
Since a native WordPress solution isn't currently available, the most effective method is to use a small JavaScript snippet. This script will detect when the Query Loop is empty and then dynamically hide the Query Total block.
Step-by-Step Instructions
- Add a Custom Class: First, you need to uniquely identify your Query Total block. Edit the block and add a custom HTML class to its Advanced settings panel. For example, you could use
custom-query-total. - Add the JavaScript Code: Insert the following code snippet into your site. You can do this by adding a Custom HTML block to your template (if your theme allows it) or by using a plugin like "Custom Code Snippets."
<script>
(function() {
// Check if the main Query Loop container has no posts
const queryLoopContainer = document.querySelector('.wp-block-query');
if (queryLoopContainer && !queryLoopContainer.querySelector('.wp-block-post')) {
// Find your specific Query Total block and hide it
const totalBlock = document.querySelector('.custom-query-total');
if (totalBlock) {
totalBlock.style.display = 'none';
}
}
})();
</script>
How it works: This script runs when the page loads. It looks for the main Query Loop block. If it finds the loop but finds no individual post blocks inside it, it then searches for your specific Query Total block (using the custom class you added) and sets its display property to 'none', effectively hiding it from view.
Important Considerations
- Theme and Plugin Conflicts: As with any custom code, there is a potential for conflict with your theme or other plugins. It's always a good practice to test this on a staging site first.
- Future Updates: This is a workaround. A future update to the WordPress block editor might introduce a native option for this, which would make this custom code unnecessary.
This approach provides a clean, user-experience-focused solution to a common design problem with the Query Loop block.
Related Support Threads Support
-
Hide Query Total block when no results are foundhttps://wordpress.org/support/topic/hide-query-total-block-when-no-results-are-found/
-
Search result page showing all posts instead of only the one searching forhttps://wordpress.org/support/topic/search-result-page-showing-all-posts-instead-of-only-the-one-searching-for/
-
Ordered List changes to unordered listhttps://wordpress.org/support/topic/ordered-list-changes-to-unordered-list/
-
Single/multiple letter from dashboard >> title?https://wordpress.org/support/topic/single-multiple-letter-from-dashboard-title/
-
Comment numbers truncated on iPhonehttps://wordpress.org/support/topic/comment-numbers-truncated-on-iphone/
-
How do I control the display order of recipes in a listhttps://wordpress.org/support/topic/how-do-i-control-the-display-order-of-recipes-in-a-list/
-
Query loop display settings not availablehttps://wordpress.org/support/topic/query-loop-display-settings-not-available/
-
Help figuring out alignment on a Pagehttps://wordpress.org/support/topic/help-figuring-out-alignment-on-a-page/
-
Rearranging information in recent postshttps://wordpress.org/support/topic/rearranging-information-in-recent-posts/
-
show the newest comments on the first pagehttps://wordpress.org/support/topic/show-the-newest-comments-on-the-first-page-2/
-
“posted by” Author name except on certain pageshttps://wordpress.org/support/topic/posted-by-author-name-except-on-certain-pages/
-
Taxonomy with double classificationhttps://wordpress.org/support/topic/taxonomy-with-double-classification/
-
Can Next Page of query loop open in different templatehttps://wordpress.org/support/topic/can-next-page-of-query-loop-open-in-different-template/
-
Block editorhttps://wordpress.org/support/topic/block-editor-30/
-
Formatting list itemshttps://wordpress.org/support/topic/formatting-list-items/
-
Custom post order query, using newsmatic elementorhttps://wordpress.org/support/topic/custom-post-order-query-using-newsmatic-elementor/
-
Request to view storage by categoryhttps://wordpress.org/support/topic/request-to-view-storage-by-category/
-
Ordering query loop block results with a custom meta fieldhttps://wordpress.org/support/topic/ordering-query-loop-block-results-with-a-customa-meta-field/
-
Adding multiple tags and tag cloudhttps://wordpress.org/support/topic/adding-multiple-tags-and-tag-cloud/
-
How to show more than 10 results on my Search Results pagehttps://wordpress.org/support/topic/how-to-show-more-than-10-results-on-my-search-results-page/
-
How to align these 4 buttons please?https://wordpress.org/support/topic/how-to-align-these-4-buttons-please/