Programmatically, with a view not using search api, we can do this to retrieve the total of results without loading the nodes / rows :
use Drupal\views\Views;
$view = Views::getView('view_name');
$view->build('display_id');
$count = $view->query->query()->countQuery()->execute()->fetchField();
But with search api we can't :
Error: Call to a member function countQuery() on null
Is there another way to do this ? I know I could do $view->get_total_rows = TRUE; and then $view->execute(); but doing this it seems that the loading of the rows decrease performance a lot ! :(
thanks
Comments
Comment #2
salvor_hardin commentedMore precisely : the problem is that I have to count total rows for each content type, not global count. That's why I make a $view->setArguments([$content_type]); and I want to just do a queryCount() and not a complete execute(). That is possible with views but it seems not with search api.
Comment #3
salvor_hardin commentedI found a workaround
Comment #4
rroblik commented@Salvor_Hardin can you please provide us for information ?
Comment #5
marcvangendI just happened to solve the same problem, before I ran into this issue :-)
This was the best I could come up with:
As you can see I did need to execute the Search API query, but I'm not executing the entire view. I did not measure the difference in performance, but if anyone does, please let me know the results.
Comment #6
mbatterton commented@marcvangend this was a great help for me thanks. We have then built on this to create this to help us set no-index no-follow tags on views and taxonomy terms that have no results for SEO.
If you think this can be improved please let me know.