Skip to main content
deleted 19 characters in body
Source Link
shanebp
  • 5.1k
  • 6
  • 27
  • 40

There are several issues in your function. Do not use backticks in the sql. Don't put quotes around field names. You use get_results with LIMIT 1 when you only need the value from a single column in a single row. And you use $meme_sku instead of... $post_sku ? Just guessing with this example, it might work...

function update_custom_post_meta_data() {
    global $wpdb;
    
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'products''product',
        'suppress_filters' => true,
        'meta_key' => '_sku'
    );

    $posts_array = get_posts( $args );
    $customTable = $wpdb->prefix.'custom_table';

    foreach($posts_array as $post) {
        $post->the_post();
        $post_id = $post->ID;
        $post_sku = get_post_meta($post_id,'_sku',true);
            
        $result = $wpdb->get_var( "SELECT custom_column1 FROM $customTable  WHERE custom_key_id = $post_sku");
        
        if ( $result != NULL ) {
              update_post_meta($post_id, 'custom_meta_key', $result);
        }

    }
}

There are several issues in your function. Do not use backticks in the sql. Don't put quotes around field names. You use get_results with LIMIT 1 when you only need the value from a single column in a single row. And you use $meme_sku instead of... $post_sku ? Just guessing with this example, it might work...

function update_custom_post_meta_data() {
    global $wpdb;
    
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'products',
        'suppress_filters' => true,
        'meta_key' => '_sku'
    );

    $posts_array = get_posts( $args );
    $customTable = $wpdb->prefix.'custom_table';

    foreach($posts_array as $post) {
        $post->the_post();
        $post_id = $post->ID;
        $post_sku = get_post_meta($post_id,'_sku',true);
            
        $result = $wpdb->get_var( "SELECT custom_column1 FROM $customTable  WHERE custom_key_id = $post_sku");
        
        if ( $result != NULL ) {
              update_post_meta($post_id, 'custom_meta_key', $result);
        }

    }
}

There are several issues in your function. Do not use backticks in the sql. Don't put quotes around field names. You use get_results with LIMIT 1 when you only need the value from a single column in a single row. And you use $meme_sku instead of... $post_sku ? Just guessing with this example, it might work...

function update_custom_post_meta_data() {
    global $wpdb;
    
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'product',
        'suppress_filters' => true,
        'meta_key' => '_sku'
    );

    $posts_array = get_posts( $args );
    $customTable = $wpdb->prefix.'custom_table';

    foreach($posts_array as $post) {
       
        $post_id = $post->ID;
        $post_sku = get_post_meta($post_id,'_sku',true);
            
        $result = $wpdb->get_var( "SELECT custom_column1 FROM $customTable  WHERE custom_key_id = $post_sku");
        
        if ( $result != NULL ) {
              update_post_meta($post_id, 'custom_meta_key', $result);
        }

    }
}
Source Link
shanebp
  • 5.1k
  • 6
  • 27
  • 40

There are several issues in your function. Do not use backticks in the sql. Don't put quotes around field names. You use get_results with LIMIT 1 when you only need the value from a single column in a single row. And you use $meme_sku instead of... $post_sku ? Just guessing with this example, it might work...

function update_custom_post_meta_data() {
    global $wpdb;
    
    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'products',
        'suppress_filters' => true,
        'meta_key' => '_sku'
    );

    $posts_array = get_posts( $args );
    $customTable = $wpdb->prefix.'custom_table';

    foreach($posts_array as $post) {
        $post->the_post();
        $post_id = $post->ID;
        $post_sku = get_post_meta($post_id,'_sku',true);
            
        $result = $wpdb->get_var( "SELECT custom_column1 FROM $customTable  WHERE custom_key_id = $post_sku");
        
        if ( $result != NULL ) {
              update_post_meta($post_id, 'custom_meta_key', $result);
        }

    }
}