Git Product home page Git Product logo

Comments (1)

Paulsky avatar Paulsky commented on August 16, 2024

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

from wp-polls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.