Ignore pages on production build if they have "unpublished: true" in frontmatter

I want to extent the blog module’s “unpublished” feature to other pages. It’s easy enough to create menus and such that skip pages that are “unpublished: true” in production, and then include them in staging with an “unpublished” marker. However, on production those pages still get built, even though they are not linked anywhere. I want production builds to skip them entirely, or perhaps even go back and delete them from the build/ folder before uploading.

I tried setting “:ignore => true” on those pages via the sitemap and via their frontmatter, and I tried using Fileutils to delete them from the build tree, but I’m not very proficient with ruby, so there’s probably something obvious I’m missing. I greatly appreciate any help.

You can run the same loop in the config.rb file. I have done something similar with limited success.

Something like:

# Gather all the posts with the unpublished metatag
unpublished = blog.articles.select{ |item| item.metadata[:page]["unpublished"] rescue false}

if unpublished
  unpublised.each do |page|
    ignore page.path
  end
end

(untested)