3
votes

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.

2
Pretty good effort as a newcomer to WP. Remember to read the manual and learning WP specific DB Query and Object Oriented way to do so. Regards.Lenin

2 Answers

4
votes

You're pretty close, the function that you defined is being run when you save the post do to the add_action reference, but the contents of the do_my_stuff() isn't quite correct.

It is recommended to use the $wpdb global variable to access the database, and even to reference tables. For example if you are using the default prefix of wp_ you would want to use $wpdb->users for the wp_users table. If it isn't a standard table but still uses the prefix, such as a plugin, you can use {$wpdb->prefix}tablename. You should also use $wpdb->prepare to help ensure that you are producing valid SQL (as well as prevent SQL injections).

Try this:

function do_my_stuff($post_ID)  {
    global $wpdb; // the wordpress database object

    // check if this is a revision, if so use the parent post_id
    if ($parent_id = wp_is_post_revision( $post_id )) $post_ID = $parent_id;

    // load the post
    $post = get_post($post_ID);

    // load postmeta values
    $scode = get_post_meta($post_ID, 'scode', true);
    $price = get_post_meta($post_ID, 'price', true);
    $variations = get_post_meta($post_ID, 'variations', true);

    // create sql, use %d for digits, %s for strings
    $sql = $wpdb->prepare("INSERT INTO {$wpdb->prefix}xxxxx (id, name, item_number, price, options_2) VALUES (%d, %s, %s, %s)", $post_ID, $post->post_name, $scode, $price, $variations);

    // run the query
    $wpdb->query($sql);
}
// add custom function to run on post save
add_action('save_post', 'do_my_stuff');
0
votes
function do_my_stuff($post_ID) {
    global $post,$wpdb;
    $tablename="wp_cart66_products";

    if($post->post_type == "post" && strlen( get_post_meta($post_ID, 'price', true))>0 )    {
        $id = $wpdb->get_var("SELECT id FROM ".$tablename." WHERE id=".$post_ID);
        $data=array(
            'id'=>$post_ID,
            'item_number'=>get_post_meta($post->ID, 'scode', true),
            'name'=>$post->post_title,
            'price'=>get_post_meta($post->ID, 'price', true),
            'options_2'=>get_post_meta($post->ID, 'variations', true)
        );

        $where = array("id" => $post_ID);

        // Possible format values: %s as string; %d as decimal number; and %f as float.
        $format=array( '%d', '%s', '%s', '%s', '%s');
        $where_format = array( '%d' );

        if($id>0){
            // update
            $wpdb->update( $tablename,$data, $where, $format, $where_format);
        }else{
            // insert
            $wpdb->insert( $tablename,$data,$format);
        }
    }
    return $post_ID;
}

add_action('publish_post', 'do_my_stuff');