• Resolved nemci7v

    (@nemci7v)


    I have a custom table in my WordPress database named “post_votes” used for voting posts.

    Whenever I publish a new post can I simultaneously add some data to that table as well?

    I’m trying to achieve this without editing any wp core files. Perhaps a function if possible.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anonymous User

    (@anonymized-3085)

    You’d need to hook into save_post with an add_action('save_post','yourfunction');

    Thread Starter nemci7v

    (@nemci7v)

    Thank you for the reply!!! I’m finally on the right track 🙂

    I tried this in my plugin file

    function test_it($post_ID)  {
    	$wpdb->insert( 'post_votes', array( 'post_id' => $post_ID, 'up' => 0, 'down' => 0 ), array( '%s', '%d' ) )
        return $post_ID;
    }
    
    add_action ( 'publish_post', 'test_it' );

    and when I activate teh plugin I get

    Parse error: syntax error, unexpected T_RETURN

    on the function.. not sure if I’m doing this right

    Thread Starter nemci7v

    (@nemci7v)

    nvm I forgot to put a semicolon.. but now when I try and publish a new post I get

    Fatal error: Call to a member function insert() on a non-object

    for

    $wpdb->insert( 'post_votes', array( 'post_id' => $post_ID, 'up' => 0, 'down' => 0 ), array( '%s', '%d' ) )

    Anonymous User

    (@anonymized-3085)

    add global $wpdb; to your function.

    Thread Starter nemci7v

    (@nemci7v)

    yes that did it! thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Adding data to table (database)’ is closed to new replies.