(k) If the defendant's motion to return property or suppress evidence is granted and the case is dismissed pursuant to Section
1385, or the people appeal in a misdemeanor case pursuant to subdivision (j), the defendant shall be released pursuant to Section
1318 if he or she is in custody and not returned to custody unless the proceedings are resumed in the trial court and he or she is lawfully ordered by the court to be returned to custody.
If the defendant's motion to return property or suppress evidence is granted and the people file a petition for writ of mandate or prohibition pursuant to subdivision (o) or a notice of intention to file a petition, the defendant shall be released pursuant to Section 1318, unless (1) he or she is charged with a capital offense in a case where the proof is evident and the presumption great, or (2) he or she is charged with a noncapital offense defined in Chapter 1 (commencing with Section 187) of Title 8 of Part 1, and the court orders that the defendant be discharged from actual custody upon bail.