Quantcast
Channel: Question and Answer » wpdb
Viewing all articles
Browse latest Browse all 44

Getting id of poll from WP database

$
0
0

So I have a php statement:

$latest_pollsid that contains a number which increases by 1 every time a specific form is submitted. I am reading this statement into several functions and for one of these functions I would like it to capture the value at the current time and not update. Essentially I want it to read the statement when the function runs, grab the number, and then display that without it changing in the future even when the value of the statement itself changes. Is there a way to do this with PHP?


The Scenario:

I am creating a post in the background when the user creates a new poll which embeds the poll they have just made with a shortcode. The shortcode uses the latest poll generally meaning that if I create a poll now and create a poll tomorrow, tomorrow both posts will show tomorrows poll. Todays post won’t show today’s poll.

Code used to create the background post:

function programmatically_create_post() {

    // Initialize the page ID to -1. This indicates no action has been taken.
    $post_id = -1;

    // Setup the author, slug, and title for the post
    $author_id = 1;
    $slug = 'example-post';
    $title = 'My Example Background Post';

    // If the page doesn't already exist, then create it
    if( null == get_page_by_title( $title ) ) {

        // Set the post ID so that we know the post was created successfully
        $pollq_question = wp_kses_post( trim( $_POST['pollq_question'] ) );
        $post_id = wp_insert_post(
            array(
                'comment_status'    =>  'open',
                'ping_status'       =>  'closed',
                'post_author'       =>  $author_id,
                'post_name'         =>  $slug,
                'post_title'        =>  $pollq_question,
                'post_status'       =>  'publish',
                'post_type'         =>  'post',
                'post_content'      =>  '[poll id="' . $latest_pollid . '"]'
            )
        );

    // Otherwise, we'll stop
    } else {

            // Arbitrarily use -2 to indicate that the page with the title already exists
            $post_id = -2;

    } // end if

} // end programmatically_create_post
add_filter( 'after_setup_theme', 'programmatically_create_post' );

PHP FUNCTION CODE:

function polls_latest_id() {
    global $wpdb;
    $poll_id = $wpdb->get_var("SELECT pollq_id FROM $wpdb->pollsq WHERE pollq_active = 1 ORDER BY pollq_timestamp DESC LIMIT 1");
    return intval($poll_id);
}

MANAGE POLLS CODE

This page manages to list all of the polls with the correct id’s but without knowing PHP very well I am struggling to interpret this:

echo "<tr id="poll-$poll_id" $style>n";
                    echo '<td><strong>'.number_format_i18n($poll_id).'</strong></td>'."n";
                    echo '<td>';
                    if($current_poll > 0) {
                        if($current_poll == $poll_id) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                        }
                    } elseif($current_poll == 0) {
                        if($poll_id == $latest_poll) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                        }
                    } else if(in_array($poll_id, $multiple_polls)) {
                            echo '<strong>'.__('Displayed:', 'wp-polls').'</strong> ';
                    }

Viewing all articles
Browse latest Browse all 44

Trending Articles