This is shown fairly explicit in the show: several members of the city guard are shown going about Kings Landing early in Season 2 (if I remember correctly), executing Robert Baratheon's illegitimate children, since they presumably have more right to the throne than Joffrey. Now in ACoK, this is much less obvious, as it is only referred to in a conversation between Tyrion and Janos, right before Tyrion dismisses Janos to the Wall. My question is this: when exactly in the timeline does the massacre of Robert's illegitimate children happen in the books? Apart from this conversation, there isn't much indication that the people of King's Landing have been distressed by this, until much later when we see their anger at Joffrey for being quite a lousy king in general. Was this massacre performed recently after Ned's execution, hence why Yoren brought Gendry with him to the Wall?
In the A Song of Ice and Fire novels, the massacre of Robert Baratheon's bastards is ordered by Queen Regent Cersei Lannister (not Joffrey) shortly before the arrival of Tyrion Lannister the capital. This was not the first time Cersei ordered the death of a bastard child of her husband; according to Littlefinger, a pair of twins Robert fathered on a servant at Casterly Rock had previously been murdered (it is unknown if the story is true).
Barra is the only one reported to die in the books, while in the show at least four others of Robert's bastards were killed too.
Gendry is spared from the massacre because Varys had foreseen this would take place and arranged his escape from the city in Yoren's caravan.
Yes, the executions took place after the death of Ned Stark(because Arya traveled with Yoren). But they happened , because Cersei was worried about the throne being taking in genereal and not because Joffrey was a bad king. She knew if the truth came out everyone was in danger.
My personal Non-canon thoughts.
I never thought that the bastards had a greater right to the throne than Joffrey. If Joffrey didn't get the throne then the throne would have been given to someone else.
Bastards are not allowed to inherit their father's lands or titles, and have no claims to the privileges of their father's House. It is up to their father on how to raise or treat them: at worst they are unacknowledged and ignored, though they may fare better and be discreetly sent funds to ensure their well-being. At best, a lord will acknowledge his bastard children (allowing them to take on one of the special bastard surnames), but send them away to one of his distant castles to be raised away from his lawful family.
I mean things might have differed as since it was a noble house, but there were many others who had stronger claims. And if the pureblood Baratheon son is the king, people are probably going to accept him and not the "bastards".And if they don't accept him its because they want to accept someone else, possibly a Barantheon brother, a Tangeryn, or maybe a Stark.
Nevertheless, as a highborn bastard carries the blood of a noble house, they may still be perceived as a potential threat by rival claimants. For this reason, King Joffrey orders the massacre of all of his father's illegitimate offspring
So unless anyone knew about Joffrey being a bastard this wasn't necessary.