The Ziwu Campaign was a military counter offensive launched in 230 by the state of Cao Wei against his rival state Shu Han during the Three Kingdoms period of China. The campaign was initiated by Wei's Grand Marshal, Cao Zhen following the numerous Northern Expeditions and more recently the battle of Jianwei.
 Following Zhuge Liang's return after the battle of Jianwei, Liu Shan issued an imperial decree to congratulate him on his recent success. He also restored Zhuge Liang to the position of Imperial Chancellor (丞相).
 While in Wei, Cao Rui summoned Cao Zhen to the imperial capital Luoyang, where he promoted him to Grand Marshal (大司馬) for his successful defense of Wei territory against Shu Han's expeditions. During this meeting, Cao Zhen proposed that in response to Shu Han multiple invasions. Cao Wei should launch their own invasion. He also said that if the various generals proceeded along several routes simultaneously then there could be great chance of success. Chen Qun prevailed against such a plan on the basic, that when Cao Cao defeated Zhang Lu, the army already met difficulty with the transport of the provisions. He further said that the Hanzhong territory makes it hard to advance and retreat with great danger for the troops along with a waste of resources. Cao Rui along with Cao Zhen discussed this matter. Cao Zhen in response changed the road that the army would follow to Ziwu. However Chen Qun again spoke against this plan. Cao Rui brought Chen Qun's arguments to Cao Zhen but he was adamant in his decision and the campaign began.
 In August 230, Cao Zhen led an army from Chang'an to attack Shu via the Ziwu Valley (子午谷). At the same time, another Wei army led by Sima Yi, acting on Cao Rui's order, advanced towards Shu from Jing Province by sailing along the Han River. The rendezvous point for Cao Zhen and Sima Yi's armies was at Nanzheng County (南鄭縣; in present-day Hanzhong, Shaanxi). Other Wei armies also prepared to attack Shu from the Xie Valley (斜谷) or Wuwei Commandery.
 When he heard of Wei recent movements, Zhuge Liang urged Li Yan to lead 20,000 troops to Hanzhong Commandery to defend against the Wei invasion. However, Li Yan did not want to leave his home base and serve under Zhuge Liang, so he refused at first until Zhuge Liang appeased him by allowing his son, Li Feng, to replace him if he did come. Li Yan finally went to Hanzhong Commandery under persuasion and pressure from Zhuge Liang.
 Lieutenant-General (偏将军) Xiahou Ba asked to be name as the vanguard of this expedition. He then led a force towards Hanzhong Commandery taking a route through the 330 km Ziwu Trail (子午道), and camped in a crooked gorge, near the Xingshi camp set up earlier by Shu general, Wei Yan. There, Xiahou Ba was identified by the local residents, who reported his presence to the Shu forces led by Li Yan. Xiahou Ba was under heavy attack. As the main army of Cao Zhen had not caught up with the vanguard. Xiahou Ba was put into a dire situation where he relied on his own valor to defend the barricades until reinforcements arrived, only then Xiahou Ba and his soldiers were able to withdraw.
 Away from the main operation, Wei Yan led some troops towards Yangxi (陽谿; southwest of present-day Wushan County, Gansu) to encourage the Qiang people to join Shu Han against Wei, Wei did the same and sent Guo Huai along with Fei Yao to counter those uprising. When both armies met on the field, Wei Yan managed to inflict a heavy defeat to the Wei forces which allowed him to rise more troops and prevented Zhang He from joining the expedition.
 Following those events, the conflict became a prolonged stalemate with few skirmishes. After more than a month of slow progress and by fear of significant losses and waste of resources, more and more officials sent memorials to end the campaign. Among them were Hua Xin, Yang Fu and Wang Su, son of Wang Lang. The situation wasn't helped by the difficult topography and constant heavy rainy weather lasting more than 30 days. After this, Cao Rui decided to abort the campaign and recalled the officers by October 230.
 Although the campaign failed, Cao Zhen's timely retreat allowed Wei to limit their losses. The leading commander of the Wei forces, Cao Zhen himself fell sick on the journey back to Luoyang and became bedridden in the subsequent months. He eventually died of illness in April or May 231. Cao Rui honoured him with the posthumous title "Marquis Yuan" (元侯).
 For his success against Guo Huai and Fei Yao. the Shu government promoted Wei Yan to Vanguard Military Adviser (前軍師) and Senior General Who Attacks the West (征西大將軍), and elevated him from a village marquis to a county marquis under the title "Marquis of Nanzheng" (南鄭侯). Li Yan was not permitted to go back to the east. Instead, he became a subordinate of Zhuge Liang to help him during his future expeditions.
 Cao Zhen's own son, Cao Shuang, would be the leading commander of a similar expedition in 244 which resulted into the battle of Xingshi.