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

  • Home
  • SEARCH
  • 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 9134285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:37:58+00:00 2026-06-17T08:37:58+00:00

I have the below function in WordPress. However, the meta key product_price is duplicated

  • 0

I have the below function in WordPress.
However, the meta key product_price is duplicated each time a post is updated.

Like this image

enter image description here

Is there any way to prevent this?

 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);
    $cny = get_post_meta($post->ID, 'price', true);



   /*Shipping rate */
    if( $cny < 50 )
        $shipping = 14.97;

    else if( $cny >= 50 && $cny < 200 )

        $shipping = 22.59;  

       else if( $cny >= 200 && $cny < 250 )

        $shipping = 24.59;

        else if( $cny >= 250 && $cny < 300 )

        $shipping = 26.60;


    else if( $cny >= 300 )
        $shipping = 29.27;  

    /*Exchange rate  CNY to EURO */

    $cny_to_euro = 0.124;
    $euro =     $cny * $cny_to_euro ;   
    $price = $euro + $shipping;
    $price = number_format($price,2);
    $data=array(
        'id'=>$post_ID,
        'item_number'=>get_post_meta($post->ID, 'scode', true),
        'name'=>$post->post_title,
        'price'=>$price,
        'options_1'=>get_post_meta($post->ID, 'variations', true),
        'shipped'=>'1',
    );
    $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', '%d');
    $where_format = array( '%d' );
    if($id>0){
        // update
        $wpdb->update( $tablename,$data, $where, $format, $where_format);
    }else{

        // insert
        $wpdb->insert( $tablename,$data,$format);
    }

    AddMetaPrice ( $post_ID ) ; 
}
return $post_ID;
          }
       function AddMetaPrice ( $post_ID ) 
          {
// init
global $post,$wpdb;

// We add the wordpress post ID and price to a meta tag
$metaKey = "product_price" ; 
$tableName = "wp_cart66_products" ; 
$metaQuery = "SELECT price FROM $tableName WHERE id='$post_ID'" ; 
$metaValue = $wpdb->get_var( $metaQuery ) ; 
$tableName = "wp_postmeta" ; 

// Do we have this post already?
if ( $id > 0 ) 
{
    // this already exists. we only need to update
    // $metaQuery = "UPDATE $tableName SET meta_value='$metaValue' WHERE                  meta_key='$metaKey' AND post_id='$id'" ; // we use wordpress's method instead
    $data = array ( "meta_value" => $metaValue ) ; 
    $where = array ( "meta_key" => $metaKey, "post_id" => $post_ID ) ; 
    $wpdb->update( $tableName, $data, $where ) ; 
}
else 
{
    // this is not created, we need to create it now (insert)
    // $metaQuery = "INSERT INTO $tableName (post_id, meta_key, meta_value)  VALUES ('$id', '$metaKey', '$metaValue')" ; // we use wordpress's method instead
    $data = array ( "post_id" => $post_ID, "meta_key" => $metaKey, "meta_value" => $metaValue ) ; 
    $wpdb->insert( $tableName, $data ) ; 
 }
    }
   add_action('publish_post', 'do_my_stuff');

I’ve read here that a ON DUPLICATE KEY statement exists. But i’m not sure if it can be implemented in the above code.

  • 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-17T08:37:59+00:00Added an answer on June 17, 2026 at 8:37 am

    Try replacing that piece of code:

    if ( $metaValue !== NULL ) 
    {
       update_post_meta($post_ID, $metaKey, $metaValue); // use built-in function instead
    }
    

    where it was:

    // Do we have this post already?
    if ( $id > 0 ) 
    {
        // this already exists. we only need to update
        // $metaQuery = "UPDATE $tableName SET meta_value='$metaValue' WHERE                  meta_key='$metaKey' AND post_id='$id'" ; // we use wordpress's method instead
        $data = array ( "meta_value" => $metaValue ) ; 
        $where = array ( "meta_key" => $metaKey, "post_id" => $post_ID ) ; 
        $wpdb->update( $tableName, $data, $where ) ; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code below: <?php function wp_copickpage() { color_option_update(); ?> <form method=POST action=>
I have the function below for my WordPress menu but every time I add
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
I have the below prog Object.prototype.inherit = function(baseConstructor) { this.prototype = (baseConstructor.prototype); this.prototype.constructor =
1.)If I call a static function like below and have to perform an additional
I have created the below function in wordpress in my themes functions.php file. I
Hi I have written below code function unique(th){ var obj = {}; for(var i
I have the function below. It gets the values from checked boxes and transfer
I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have the following function below: public function setupHead($title){ $displayHead .='<!DOCTYPE html PUBLIC -//W3C//DTD

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.