Whenever the court before whom any matter arising under this chapter is pending, deems publication of any matter before the court contrary to public policy or injurious to the interests of children or to the public morals, the court may by order close the files or any part thereof in the matter and make such other orders to protect the privacy of the parties as is necessary.