Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: dynamic poll #153

Open
Paulsky opened this issue May 19, 2023 · 1 comment
Open

Question: dynamic poll #153

Paulsky opened this issue May 19, 2023 · 1 comment

Comments

@Paulsky
Copy link

Paulsky commented May 19, 2023

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?

  • Not interesting
  • Interesting
  • Very interesting

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!

@Paulsky
Copy link
Author

Paulsky commented May 19, 2023

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant