During Emperor Xizong's reign
Later, when Gao Shi (高湜) oversaw the office of imperial censors (御史臺, Yushi Tai), he had Wang Hui recalled to Chang'an to serve as a supervising censor Shi Yushi Zhiza (侍御史知雜), as well as Zhifang Yuanwailang (職方員外郎), a low-level official at the ministry of civil service affairs (吏部, Libu). From that latter position, Wang was later promoted to Kaogong Langzhong (考功郎中), a supervisory official at the ministry of civil service affairs. The chancellor Xiao Fang particularly respected Wang for his administrative abilities. Wang was later made Sifeng Langzhong (司封郎中), still a supervisory official at the office of civil service affairs, as well as the magistrate of Chang'an County (one of the two counties making up the capital), but as at that time, the ranks of imperial scholars (翰林學士, Hanlin Xueshi) was severely lacking, Xiao had him made an imperial scholar and also Zhifang Langzhong (職方郎中), also a supervisory official at the ministry of civil service affairs; Wang was also put in charge of drafting edicts. He was later made Zhongshu Sheren (中書舍人), a mid-level official at the legislative bureau. Yet later, he was made chief imperial scholar (翰林學士承旨, Hanlin Xueshi Chengzhi) and deputy minister of census (戶部侍郎, Hubu Shilang). While serving as chief imperial scholar, he also successively served as deputy minister of defense (兵部侍郎, Bingbu Shilang) and then Shangshu Zuo Cheng (尚書左丞), one of the secretaries general of the executive bureau (尚書省, Shangshu Sheng).
In late 880, as the major agrarian rebel Huang Chao was approaching Chang'an, the powerful eunuch Tian Lingzi, in order to deflect blame for the imperial forces' inability to stop Huang, blamed the leading chancellor Lu Xi for erroneous strategies and had Lu demoted. (Lu, in response, committed suicide.) Wang and Pei Che were made chancellors (with the designation Tong Zhongshu Menxia Pingzhangshi (同中書門下平章事)) to replace Lu. However, on the same day, news arrived that Huang was about to attack Chang'an. Tian took Emperor Xizong and fled toward Chengdu, and the officials scrambled to either try to follow the emperor or to hide. Wang tried to follow the emperor in flight, but in his attempt to do so fell into a bush in a valley and was injured. He was captured by Huang's soldiers, who took him back to Chang'an, where Huang had declared himself emperor of a new state of Qi. Huang tried to force Wang to accept a commission in the Qi government, but Wang pretended to have been so severely injured that he could not speak, even when swords were placed against him. Huang had Wang returned to his mansion to recuperate, under close watch, and he sent doctors to treat Wang. After about a month, though, when the guards had relaxed, Wang disguised himself as a merchant and fled to Hezhong (河中, in modern Yuncheng, Shanxi). He submitted a report to Emperor Xizong (who was then at Chengdu), and Emperor Xizong had him remotely made the minister of defense (兵部尚書, Bingbu Shangshu).
Wang was then planning to join the emperor in Chengdu, but he was then commissioned to review the Tang troops in the east. At that time, the chancellor Wang Duo was in overall command of the operations against Huang, but Tang forces had not been able to gain a decisive victory over Qi. The eunuch monitor of the army, Yang Fuguang wanted to invite the Shatuo rebel Li Keyong to join the Tang cause, and Wang Hui agreed with the proposal. Wang Duo then issued an edict in Emperor Xizong's name pardoning Li Keyong and summoning him to join the Tang imperial cause. After Tang forces, with Li Keyong as a major contributor, defeated Huang and recaptured Chang'an early in 883, Wang, for his contribution, was made You Pushe (右僕射), one of the heads of the executive bureau.
Meanwhile, Zhaoyi Circuit (昭義, headquartered in modern Changzhi, Shanxi) had been going through much turmoil—as the officer Cheng Lin (成麟) had killed the military governor Gao Xun (高潯) in a mutiny in 881 and was in turn killed by another officer, Meng Fangli. The people of Zhaoyi then asked the eunuch monitor Wu Quanxu (吳全勗) to act as military governor, but Meng rejected this (claiming that a eunuch should not be a governor) and put Wu under house arrest, instead claiming that he wanted the imperial government to send a replacement. The imperial government commissioned Wang Hui to serve as Zhaoyi's military governor. Wang, however, knowing that Meng had full control of three of Zhaoyi's five prefectures (i.e., the ones east of the Taihang Mountains), believed that he would not be actually able to exercise control, and so declined the commission. The circuit was then instead given to Zheng Changtu, although Zheng left his post just three months later, leaving Meng in control.
At this time, Chang'an was in shambles due to the warfare, and Emperor Xizong had not returned to Chang'an. Wang was made the director of Daming Palace (大明宮) and acting mayor of Jingzhao, overseeing the recovery effort. It was said that Wang comforted the people, such that the people began to return. However, the powerful individuals inside the palace (i.e., eunuchs headed by Tian) and outside were trying to reconstruct their mansions, and they often infringed on the people's rights. When the people complained, Wang tried to exercise the laws properly against the powerful people, and thus offended them. They thus had another official, Xue Qi (薛杞), made Wang's deputy, with the intent that Xue would strip Wang's actual authority, but Wang, citing the fact that Xue's father had just died and that he should thus be observing a period of mourning, refused to let Xue take office. This further angered the powerful people, and Wang was stripped of his posts and summoned to Chengdu. He was then made a senior advisor to the Crown Prince—an entirely honorary post as there was no crown prince at the time—and ordered to retire to Hezhong. About three months later, even that honorary post was stripped, although a similar post was conferred after Emperor Xizong returned to Chang'an in 885, and Wang was recalled to Chang'an, although he declined the post on account of illness. The chancellors, rebuking Wang for not accepting the imperial largess, had him made the prefect of Ji Prefecture (集州, in modern Bazhong, Sichuan) and ordered him to report immediately.
Immediately after, though, Tian provoked Wang Chongrong and Li Keyong into attacking Chang'an, and Emperor Xizong fled to Xingyuan (興元, in modern Hanzhong, Shaanxi). The warlord Zhu Mei, the military governor of Jingnan Circuit (靜難, headquartered in modern Xianyang, Shaanxi), took over Chang'an and declared an imperial prince, Li Yun the Prince of Xiang, emperor. Tian was removed from his post and fled to Chengdu to serve as the eunuch monitor of Xichuan Circuit (西川, headquartered in modern Chengdu), where his brother Chen Jingxuan was military governor. After Tian's removal, Emperor Xizong sent an edict summoning Wang to serve as the minister of civil service affairs (吏部尚書) and creating him the Marquess of Langye. Wang wanted to report to Emperor Xizong, but the roads were blocked off by Li Yun. Subsequently, Li Yun's edict arrived at Hezhong, also summoning Wang. Wang claimed to have suffered a stroke and to be unable to walk, and he refused to submit to Li Yun. After Zhu was killed by his own subordinate Wang Xingyu later in the year, and Li Yun was then executed by Wang Chongrong when he fled to Hezhong, Emperor Xizong returned to Chang'an. He made Wang chief imperial censor (御史大夫, Yushi Daifu), but Wang declined on account of illness.