Can we get full count of results from a query that is extended by PagerDefault I have a query that is extended by PagerDefault.  I need to get the total count of results to show something like: "There are a total of 100 results, now showing 21-40".  I can't figure out where to get that number without building a separate query to run that doesn't use PagerDefault.

First place I looked was theme_pager(), which I'm already overriding in template.php.  The total result count is not passed there, only the current page (as $variables['element']).

I see that within includes/pager.inc in pager_default_initialize() only the current page number is sent to theme_pager, although the total items are passed to that function. (So close!)

Then I thought I could clone the query, but the query must be extended prior to adding conditions, so that wouldn't work.  I'll note that this isn't ideal either, because ideally, I would be able to get this total count without two db hits (as it would hit it for the count every time a user navigates the pagination).

So, does anyone know the best way to get the total count?

Thanks!
With the existing pager class I don't think you can get that data without running the count query again. The total count is queried directly and saved to a local var before being passed off to pager_default_initialize()...it's not saved anywhere.

One option would be to extend the default class and use that for your pager queries. This is un-tested but I think it'll do the trick

class MyPager extends PagerDefault {

  /**
   * Overrides PagerDefault::execute().
   */
  public function execute() {
    // Add convenience tag to mark that this is an extended query. We have to
    // do this in the constructor to ensure that it is set before preExecute()
    // gets called.
    if (!$this-&gt;preExecute($this)) {
      return NULL;
    }

    // A NULL limit is the "kill switch" for pager queries.
    if (empty($this-&gt;limit)) {
      return;
    }
    $this-&gt;ensureElement();

    // Get the total items from our new statically cached function.
    $total_items = $this-&gt;getTotalItemCount();

    $current_page = pager_default_initialize($total_items, $this-&gt;limit, $this-&gt;element);
    $this-&gt;range($current_page * $this-&gt;limit, $this-&gt;limit);

    // Now that we've added our pager-based range instructions, run the query normally.
    return $this-&gt;query-&gt;execute();
  }

  /**
   * Cache the results of PagerDefault::getCountQuery().
   */
  public function getTotalItemCount() {
    static $total_items = NULL;

    if ($total_items === NULL) {
      $total_items = $this-&gt;getCountQuery()-&gt;execute()-&gt;fetchField();
    }

    return $total_items;
  }

}


You could then construct a query like the following

$query = db_select('table', 't')
  -&gt;fields('t')
  -&gt;condition('foo', 'bar')
  -&gt;extend('MyPager');

$current_items = $query-&gt;execute();
$total_items = $query-&gt;getTotalItemCount();


There might be an easier way, but I just can't see it.