Getting id of poll from WP database

The first problem I spotted is that you aren’t setting $latest_pollid but you are nonetheless trying to use that as the poll id. You should have something like: $latest_pollid = polls_latest_id(); before that line in order to set the value.

There are big problems with what you are doing though. Hooking to after_setup_theme means that your code runs on every page load. You are restricting most of it to the page titled ‘My Example Background Post’ but I still suspect you are getting a disconnect between the quiz and the post you are creating. I don’t know how to solve this unless the quiz plugin has a hook you can use, or perhaps the $_POST data has information that could be relevant.