I would like to run an sql query whenever i publish a post. Im just not sure how to wrap it in a function and where to paste that function to take effect that when i click publish. The following command, which is the one i need, runs successfully in phpMyAdmin- SQL, and it also reflects correctly in my wordpress backend.
INSERT INTO `databasename`.`wp_xxxxxx` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', 'xxxx1', 'xxxx2', 'xxxxx3', 'xxxx4'
);
But, how do i get this to run in PHP, in my functions.php file when i save a post each time? I tried the following, but i'm sure its correct because i do a refresh afterwards and the tables are not created:
function do_my_stuff($post_ID) {
mysql_query("INSERT INTO `databasename`.`wp_xxxxx` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', 'xxxx1', 'xxxx2', 'xxxx3', 'xxxxx4'");
return $post_ID;
}
add_action('save_post', 'do_my_stuff');
When a post is published. It is actually a product that needs some details.
Here are my desired VALUES for the sql query to populate.
- id = the postID number of the post.
- name = the post title
item_number = it should be a custom field post meta value, coming from a field named 'scode', at the moment i echo it on my single page template via:
ID, 'scode', true) ) echo do_shortcode(get_post_meta($post->ID, 'scode', $single = true)); ?>price = the price, which i also manually enter via a custom field called 'price' which i currently echo via :
options_2, which i also manually enter via a custom field value called 'variations'.
Can all this be done like in this way?
Perhaps something like:
INSERT INTO `databasename`.`wp_cart66_products` (
`id` ,
`name` ,
`item_number` ,
`price` ,
`options_2`) VALUES (
'9', '<?php the_title(); ?>', '<?php if ( get_post_meta($post->ID, 'scode', true) ) echo do_shortcode(get_post_meta($post->ID, 'scode', $single = true)); ?>', '<?php $values = get_post_custom_values("price"); echo $values[0]; ?> ', '<?php $values = get_post_custom_values("variations"); echo $values[0]; ?> '
);
Please note that i don't very little about php, or SQL. I spend hours trying to find as much possible information as i can to provide a solid request.