Counting Draft Page Children in a Page Model

I’m working on some page model functions to integrate to a stats section in the panel and am either doing something wrong or maybe have run into a limitation or bug. Wondering if anyone can set me straight on this:

I have a page model file notes.php and in it is two main functions:

<?php 
  // site/models/notes

  class NotesPage extends Page {
    // Get all Notes
    public function getAllNotes() {
      $published_notes = $this->children();
      return $published_notes;
    }

    // Count the total number of notes
    public function getAllNotesCount() {
      $all_notes = $this->getAllNotes()->count();
      return (int) $all_notes;
    }

    // Count the toal number of unpublished notes
    public function getUnpublishedNotesCount() {
      $unpublished_notes_count = $this->getAllNotes()->drafts()->count();
      return (int) $unpublished_notes_count;
    }
}

The first two functions work but the last to get a count of drafts does not. Is this not supported or am I doing something wrong?

info: '{{ site.page("notes").drafts.count }} drafts

I do know that the above line in a blueprint as part of a stats section does work, but maybe I’m mistakenly assuming the same query should work in a model context.

You are indirectly calling children()->drafts(). It should be either drafts (which are children) or just children. In the second and third functions of your class, try $this->drafts() instead of $this->getAllNotes().

1 Like
$page->children()

Listed and unlisted (=published) child pages

$page->drafts()

Draft child pages

$page->childrenAndDrafts()

Both published and draft child pages

2 Likes