Ready to get started?Download WordPress


save_post how to find post_id? (3 posts)

  1. macelfresh
    Posted 4 years ago #


    I'm writing here because I have a stupid problem and somehow I can't find a solution on the internet.

    So here's the thing, I have a rating system I'd like to set up on some posts when I publish them.
    Basically, the idea is to initiate their presence in the new table I set up in my database.

    I ran a test in the front end, my function works

    function register_rating(){
    	global $post, $wpdb;
    	$vote = 0;
    	$total = 0;
    		$query_verif = 'SELECT * FROM wp_ratings WHERE post_id = '. $post->ID;
    		$r = $wpdb->get_results($query_verif);
    		if (empty($r)){
    			$query_reg = 'INSERT INTO wp_ratings (key_rating, post_id, nb_vote, total) VALUES ("",'. $post->ID.','. $vote.','. $total.')';

    It does create the new field in the database.
    Now, for some reason of proper working and not saturating the front-end with loads of queries, I'd like this to happen when I save the post in the admin panel.

    so I hooked it:

    add_action( 'save_post', 'register_rating' );

    but...this doesn't work. Nothing is happening.

    For testing purposes, I changed the hook on 'admin_menu', so I could display the query. It turns out that there is no id after the '='.

    What am I doing wrong? How do I get this to work? There is so few info on the internet on this matter of getting the postid in the save_post...

    Thanks a lot for your help


  2. s_ha_dum
    Posted 4 years ago #

    Change your function declaration to function register_rating($postid). Then use $postid for your post ID. Also, you don't want to be using $post. You want to be using $_POST.

  3. macelfresh
    Posted 4 years ago #


    sorry I didn't reply before, I didn't have time to work on that again.

    Thanks for the answer, it now works !

    Tout de bon


Topic Closed

This topic has been closed to new replies.

About this Topic