Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:59:18+00:00 2026-06-11T11:59:18+00:00

I would like to run an sql query whenever i publish a post. Im

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:59:19+00:00Added an answer on June 11, 2026 at 11:59 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to run a SQL query that returns events
I would like to run a (extensive) query that produces one line of XML.
I would like to run an SQL query on an iSeries (...or System i
I would like to run the following SQL select: SELECT ID, NUMERATOR, (SELECT m.COLUMNNAME
I would like to run this script (embed drive list in a site) that
I would like to run a LINQ query like this: var words = from
i am trying to run a sql query which will not show distinct/duplicate values.
I have a sql query that I run against a sql server database eg.
I have two SQL queries that I would like to combined into one, if
This query is supposed to run with ms access 2003 using SQL. the function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.