Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am making a voting system

The problem is that the post id Is returning blank

I am sure that post id is returning blank

Bcoz when I put a real post id value there it seems working

For example

update_post_meta(123, "votes" , $newvotes)

Is Working properly

But

 update_post_meta($postid, "votes" , $newvotes)

isn't working properly

here is the code

  add_action("wp_ajax_add_votes_options", "add_votes_options"); 
  add_action("wp_ajax_nopriv_add_votes_options", "add_votes_options");
  function add_votes_options() {
if (!wp_verify_nonce($_POST['nonce'], 'voting_nonce'))
    return;

$postid = $_POST['postid'];
$ip = $_POST['ip'];

$voter_ips = get_post_meta($postid, "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
    echo "null";
    die(0);
} else {
    $voter_ips[] = $ip;
    update_post_meta($postid, "voter_ips", $voter_ips);
}   

$current_votes = get_post_meta($postid, "votes", true);
$new_votes = $current_votes + 1;
update_post_meta($postid, "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
die(0);
    }

AND ALSO I TRIED THIS

add_action("wp_ajax_add_votes_options", "add_votes_options");
add_action("wp_ajax_nopriv_add_votes_options", "add_votes_options");
function add_votes_options() {
if (!wp_verify_nonce($_POST['nonce'], 'voting_nonce'))
    return;

$postid = $_POST['postid'];
$ip = $_POST['ip'];

$voter_ips = get_post_meta($_REQUEST["post_id"], "voter_ips", true);
if(!empty($voter_ips) && in_array($ip, $voter_ips)) {
    echo "null";
    die(0);
} else {
    $voter_ips[] = $ip;
    update_post_meta($_REQUEST["post_id"], "voter_ips", $voter_ips);
}   

$current_votes = get_post_meta($_REQUEST["post_id"], "votes", true);
$new_votes = intval($current_votes) + 1;
update_post_meta($_REQUEST["post_id"], "votes", $new_votes);
$return = $new_votes>1 ? $new_votes." votes" : $new_votes." vote";
echo $return;
die(0);
    }

But none of this codes are working

EDIT#1 calling my action at jQuery

$("#vote").not(".disabled").click(function() {
    var el = $(this);
    el.html('<span id="loader"></span>');
    var nonce = $("input#voting_nonce").val();
    var data = {
        action: 'add_votes_options',
        nonce: nonce,
        postid: '<?php echo $post->ID; ?>,',
        ip: '<?php echo $_SERVER['REMOTE_ADDR']; ?>'            
    }; 
share|improve this question
    
What values do you get whe you print_r your $_POST parameters? Is everything correct there? –  fischi Dec 20 '12 at 9:45
    
Yes. But still post id is returning blank –  vaibhav Dec 20 '12 at 9:48
    
Could you please add the output of your print_r to your question, so I can take a look at it? –  fischi Dec 20 '12 at 9:49
    
Am in my mobile so I can't add there .is there any other way to get post id –  vaibhav Dec 20 '12 at 9:54
    
I'm sorry, but I can't help if I have to guess about the information.. –  fischi Dec 20 '12 at 9:57

1 Answer 1

On a default install of WP you get always the ID of a post with the function get_the_ID(). Id dosnt work this you go about the global var: $post.

echo $GLOBALS['post']->ID

Is this not enough, then go about the global wp_query

$GLOBALS['wp_query']->post->ID;
share|improve this answer
    
Get_the_id() functions only works within the loop –  vaibhav Dec 20 '12 at 10:35
    
right, for outside the loop the other variants. –  bueltge Dec 20 '12 at 11:35
    
You are right @bueltge, but as far as I got this question, the ID comes from an AJAX call, so it won't be set. –  fischi Dec 20 '12 at 12:00
    
It isn't working @bueltge –  vaibhav Dec 20 '12 at 15:24
    
@fischi please check the edit may it help you –  vaibhav Dec 20 '12 at 16:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.