Open Paulsky opened 1 year ago
After some research, I believe there is no relation between a poll and a post. So I don't think this is possible. Please correct me if I'm wrong.
So, I created a script which will check if there is a poll id attached to the post. If not, it will check if there is a poll with the same title. If not, it will create a poll and attach it to the post meta. Please see this template script, maybe it could help someone:
function get_or_create_wp_poll_id_for_post($postId)
{
$pollId = a_function_to_get_post_meta('poll_id', $postId);
if (empty($pollId)) {
global $wpdb;
$title = get_the_title($postId);
$pollTitle = What rating would you give ' . $title . '?';
$pollId = $wpdb->get_var("SELECT pollq_id FROM " . $wpdb->pollsq . " WHERE pollq_question = '" . $pollTitle . "' LIMIT 1");
if (!$pollId) {
$newQuestion = $wpdb->insert(
$wpdb->pollsq,
[
'pollq_question' => $pollTitle,
'pollq_timestamp' => current_time('timestamp'),
'pollq_totalvotes' => 0,
'pollq_active' => 1,
'pollq_expiry' => 0,
'pollq_multiple' => 0,
'pollq_totalvoters' => 0
],
[
'%s',
'%s',
'%d',
'%d',
'%d',
'%d',
'%d'
]
);
if ($newQuestion) {
$pollId = (int)$wpdb->insert_id;
a_function_to_set_post_meta($id, 'poll_id', $pollId);
$answers = ['Not interesting', 'Interesting', 'Very interesting'];
foreach ($answers as $answer) {
$wpdb->insert(
$wpdb->pollsa,
[
'polla_qid' => $pollId,
'polla_answers' => $answer,
'polla_votes' => 0
],
[
'%d',
'%s',
'%d'
]
);
}
}
}
}
return $pollId;
}
I would like to display a poll for each of my posts. Example poll would look like something like this:
What rating would you give
POST TITLE
?My question is: (how) can I create one poll and use it for multiple posts?
If this is not possible, how can I create a poll dynamically? Something like; 'if no poll for this post exist, create one, save the id in the post meta'.
Thank you in advance!