WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm basically trying to have a copy of a post be sent to another database's table when it's published. I've gotten it to add a new row in the new table, with my own static arguments, but I'm not sure how to reference the actual post that's being published.

function run_when_post_published($post_ID)
    echo "Hellooooo? Steve?";
    echo "<br /> POST ID: " . $post_ID; // Won't even display "POST ID" with $post_ID in the string.
    $con = new wpdb("password", "_________admin", "_______wp", "_______.com");
    if (!$con)
        die('Could not connect: ' . mysql_error());

    mysql_select_db("wp", $con);

    $result = mysql_query("INSERT INTO ________wp.featured_posts (post_domain, post_author, post_title, post_content)
VALUES ('3', '2', 'Sent from hook','Hello')");


add_action('new_to_publish_featured', 'run_when_post_published');       
add_action('draft_to_publish_featured', 'run_when_post_published');     
add_action('pending_to_publish_featured', 'run_when_post_published');

add_action('new_to_publish', 'run_when_post_published');        
add_action('draft_to_publish', 'run_when_post_published');      
add_action('pending_to_publish', 'run_when_post_published');

I'd like to have the whole post just basically copied.

Also I'd need to do this with attachments using:


Again I've gotten the add_attachment action hook to add a row in the database, but I just don't know how to reference the attachment that the hook is being initiated from.

share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.