President Trump loves Fox News, the network that boosted his presidential ambitions and has often acted as a sword and shield against his critics. And, lately, he also hates Fox News.

Such as Sunday night, when Trump dashed off a tweetstorm that slammed NBC, MSNBC, the New York Times, CNN — and Fox.

“Watching @FoxNews weekend anchors is worse than watching low ratings Fake News @CNN,” the media critic in chief said. He added, “But @FoxNews, who failed in getting the very BORING Dem debates, is now loading up with Democrats & even using Fake unsourced @nytimes as . . . a ‘source’ of information . . . @FoxNews is changing fast, but they forgot the people who got them there!”

(He was referring to network stories about a Times article that revealed harrowing conditions at a U.S. border-detention facility.)

The president went on to complain about how Fox in March hired Donna Brazile, the former interim Democratic National Committee chair, as a paid pundit and how she makes appearances on Fox anchor Shepard Smith’s daily program. (Brazile has never actually appeared on Smith’s show.)

Trump’s love-hate relationship with Fox is actually fairly consistent. Despite doling out interviews to Fox programs and hosts, despite often praising the network, and despite hiring some of its key figures for important posts in his administration, he maintains a compartmentalized vitriol. It is reserved for the network’s news reporting, not for the opinion hosts who regularly praise him.

Sunday’s rant wasn’t Trump’s first against Fox, which was instrumental in turning the ­reality-TV-show host into a viable political contender. In 2011, the network gave Trump a regular slot on its morning show “Fox & Friends.” He used it to promote his discredited “birtherism” conspiracy against President Barack Obama and to test-drive other themes that would become part of his presidential campaign starting in 2015.

In June, he tweeted his irritation with a Fox poll that showed him losing to five Democratic candidates in 2020. (“Something weird going on at Fox,” he wrote, tagging his tweet with a tweak at Fox anchor Bret Baier, “More Fake News @BretBaier.”)

He raged, too, in April when Fox produced a town-hall-style program featuring Sen. Bernie Sanders (I-Vt.), while taking another shot at Baier (“Not surprisingly, @BretBaier and the ‘audience’ was so smiley and nice. Very strange.”).

And in March, he slammed two of Fox’s weekend anchors, throwing another elbow at Smith in the process.

At the same time, Trump has granted interviews to Fox’s stable of Trump-loving hosts, including Laura Ingraham, Jeanine Pirro, “Fox & Friends’s” Steve Doocy and Ainsley Earhardt, Jesse Watters, Mark Levin, Tucker Carlson, Lou Dobbs and Sean Hannity. The latter three practically form a shadow cabinet, offering regular private advice to the president on immigration, foreign policy and the economy.

Trump typically returns the on-air affection of the Fox hosts with bouquets of his own.

When Fox suspended Pirro for two weeks in May for comments disparaging Rep. Ilhan Omar (D-Minn.), for example, Trump came to her defense, tweeting, “Bring back @JudgeJeanine Pirro. The Radical Left Democrats, working closely with their beloved partner, the Fake News Media, is using every trick in the book to SILENCE a majority of our Country. They have all out campaigns against @FoxNews hosts who are doing too well.”

And then, playing TV programmer, he added, “Fox . . . must stay strong and fight back with vigor. Stop working soooo hard on being politically correct. . . . The losers all want what you have, don’t give it to them. Be strong & prosper, be weak & die!”

Fox News’s representatives didn’t respond to requests for comment Monday. But people inside and outside the network see no major break in the Fox-Trump nexus.

“I think the president still loves Fox News,” said Christopher Ruddy, a Trump friend who is chief executive of the conservative Newsmax news site and TV network, a smaller rival to Fox. “He thinks highly of [Fox Corp. chairman] Rupert Murdoch. He just likes to negotiate. He’ll be critical when he thinks they’re not being fair to him. But I don’t think he’s abandoning Fox or trying to undermine them.”

Trump, Ruddy said, is being “situational and tactical,” but the overall relationship is sound: “I think he’s happy with Fox.”

One person intimate with Fox’s operations called the president’s venting “the same ol’ same ol’. Trump expects 168 hours a week of ­pro-coverage on Fox. A slight deviation, [and] he rattles their cage.” Speaking on the condition of anonymity to not jeopardize their relationship with Fox, this person said it was likely Fox “will fall back in lockstep” as the campaign season heats up.