i'm writing a little action plugin. everytime a new post is created, a new table should be created in an external db. The name of the table sholud reflect the post_id of the newly created post, but i can0t figure how to pass it.

Here's my code so far (for php purists, forgive me for using old mysql_connect (but adding the flag TRUE to the new_link parameter, i don't have to bother with wpdb globals etc)

function insert_new_table($post_id){
    $usernamel = "secret";
    $passwordl    = "secret";
    $hostl    = "localhost";
    $databasel    ="secret";
    $connect = mysql_connect($hostl, $usernamel, $passwordl, TRUE) or die("Error");
    mysql_select_db($databasel) or die("Error, Cannot locate the database!");
    $table_name = 'tablename4';  //with this variable works
    $postidl = get_the_ID();     // i'm outside the loop .. so what?
    $sql = "CREATE TABLE $postidl (
      id int(11) NOT NULL AUTO_INCREMENT,
      name varchar(255) DEFAULT NULL,
      UNIQUE KEY id (id)
    $query = mysql_query($sql);
    return $post_id;    
add_action( 'wp_insert_post', 'insert_new_table' );

everything is going smooth using the var $table_name in my $sql query; but i can't pass the post_id of the post that is currently being saved (don't know how to get it!)

Any hint?

  • Why would you want this? You should use your own wpdb object if you don't want to use the global $wpdb; object instead of calling mysql functions ( the mysql extension in PHP is deprecated, WordPress uses mysqli in PHP 5.5 )
    – Tom J Nowell
    May 12, 2014 at 13:28

2 Answers 2


You shouldn't be calling get_the_ID() outside of a post loop, and the post ID is handed to you using the filter as a function argument:

function insert_new_table( $post_id ){

So use $post_id

Added notes

  • Don't use mysql_connect etc that extension was deprecated and is no longer included by default in newer versions of PHP. This code will fail on PHP 5.5+, if you must abandon the WP APIs use PDO or Mysqli
  • You don't have to use the global wpdb object, you can create your own: $mydb = new wpdb( $dbuser, $dbpassword, $dbname, $dbhost );
  • You're creating a table for each post in a remote database with a name and an ID column, this is wasteful and bad table design. Would it not make more sense to have a single table containing all names with a post ID in it? Why must it be in a separate database? What happens if your IDs change? Why not use custom post types/post meta/custom taxonomies? You realise there'll be a significant latency cost if your database is located elsewhere? Your data will not survive a site move or import/export
  • Pick an indenting method and stick to it, your editor should be capable of automatically fixing and indenting without any effort from you, if it doesn't you should find a new one. I would recommend sublimetext or PHPStorm but others exist. WordPress standards use tabbed indenting taking up 4 spaces on the screen. PSR standards use 4 space characters.
  • You're not checking for drafts and revisions
  • Would +2 if I could ;) May 12, 2014 at 13:44
  • Sorry, i could explain better, i'm not using wordpress as a blog engine, just as a sort of dashboard manager and it's the first time i write here to ask for support (that's why indentation looks ugly, i use sublime text and i know psr) May 12, 2014 at 19:25
  • about the fact i'm not checking for draft and revision, i read here codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_post that "The wp_insert_post action is called with the same parameters as the save_post action (the post ID for the post being created), but is only called for new posts and only after save_post has run". May 12, 2014 at 19:31
  • the query was not working when i first tried with $post_id. post_id is numeric and mysql is a bit snob about table names May 12, 2014 at 20:36

The hook is called with the post ID as the first (and only) argument, you don't need to fetch/retrieve it, it's being sent to your function:

function insert_new_table( $post_id ) {
  //                 here  ^^^^^^^^
  # ... your code ...
  $sql = "CREATE TABLE {$post_id} (
    name varchar(255) DEFAULT NULL,
    UNIQUE KEY id (id)
  # ... more of your code ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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