WordPress.org

Ready to get started?Download WordPress

Forums

Copy post to separate database when published with 'add_action(...)' hook (1 post)

  1. Isaac Rector
    Member
    Posted 1 year ago #

    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')");
    
        	mysql_close($con);
        }
    
        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. So basically I'm asking how to properly use the $post_ID arg, or how can I use $post_ID to access the post information (author, title, etc).

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

    add_action('add_attachment',....);

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic