Short answer. The court may require it, at its discretion. Section 22 of Rule 3 allows the court, in any action involving the validity of a treaty, law, ordinance, executive order, presidential decree, or rules and regulations, to require the Solicitor General's appearance so the government can be heard.
What the law says
In any action involving the validity of any treaty, law, ordinance, executive order, presidential decree, rules or regulations, the court, in its discretion, may require the appearance of the Solicitor General who may be heard in person or through a representative duly designated by him.
Rule 3, Section 22 — Notice to the Solicitor General. Read the full provision →
What kinds of cases this covers
Section 22 applies broadly to actions 'involving the validity' of several categories of government issuances: treaties, laws, ordinances, executive orders, presidential decrees, and rules or regulations. A case need not be a formal constitutional challenge in name — any action where the validity of one of these instruments is genuinely at issue can trigger this section, including a case attacking a city or municipal ordinance. A city ordinance falls squarely within that list because an ordinance is the kind of local legislative enactment the section's reference to 'ordinance' is meant to capture, placing a challenge to one on the same footing as a challenge to a national law or presidential decree for purposes of this section.
It is discretionary, not automatic
The section uses permissive language: the court 'in its discretion, may require the appearance of the Solicitor General.' Notice to the Solicitor General is not mandatory in every case that happens to touch on the validity of a government issuance — the court decides, based on the circumstances, whether the government's participation would be useful or necessary to fairly adjudicate the validity question raised. A litigant who wants to be sure the government weighs in cannot compel that appearance as a matter of right; the most that can be done is to ask the court to exercise its discretion in favor of requiring it.
How the government's voice enters the case
When the court does require it, the Solicitor General 'may be heard in person or through a representative duly designated by him.' This gives the government's chief legal counsel a mechanism to defend the validity of the challenged law, ordinance, or issuance, ensuring the court hears the state's position on a question that could affect the enforceability of government action well beyond the two private litigants before it. Allowing a duly designated representative to appear instead of the Solicitor General personally means the office is not required to divert its own leadership to every case implicating the validity of a government issuance — a representative can be sent instead, without weakening the government's ability to be heard on the question.