Woocommerce get Product id using product SKU

Woocommerce get Product id using product SKU

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Product IDs from SKUs in WooCommerce

The code snippet you provided attempts to instantiate a WC_Product object directly using the SKU string as an argument. This approach fails because the WC_Product constructor expects a numeric product ID, not a textual identifier like a SKU. Passing a SKU string results in an invalid object or null values, preventing you from accessing properties like the price.

To resolve this, you must first query the database to find the numeric ID associated with that specific SKU before instantiating the product class. In WooCommerce, products are stored in the wp_posts table with post_type set to 'product'. The actual SKU data resides in the meta_values table or within post meta depending on how it was added.

Querying by Post Meta

The most efficient and standard way to retrieve a product ID from a custom field (post meta) is to use WordPress's global $wpdb object or the cleaner get_post_meta function combined with a query. Since you are already using Advanced Custom Fields (acf) based on your get_field usage, the data is likely stored in the ACF database table, but for standard WooCommerce operations, querying the wp_postmeta table is often more robust.

You can use a direct SQL query via $wpdb to find the product ID where the meta key matches your SKU field and the meta value matches your input:

global $wpdb;

$sku = get_field( 'product_sku' );

if ( ! empty( $sku ) ) {
    // Prepare variables to prevent SQL injection
    $sql = "SELECT ID FROM {$wpdb->prefix}posts 
            INNER JOIN {$wpdb->prefix}postmeta ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id 
            WHERE {$wpdb->prefix}posts.post_type = 'product' 
            AND {$wpdb->prefix}postmeta.meta_key = '_%s' 
            AND {$wpdb->prefix}postmeta.meta_value = %s";
    
    // Note: ACF often uses 'acff_%s' or similar, but standard WC meta is '_sku'.
    // If your SKU is stored in a specific meta key like 'product_sku', use that.
    
    $args = array( 'product_sku', $sku );
    $product_id = $wpdb->get_var( $wpdb->prepare( $sql, ...$args ) );

    if ( $product_id !== false ) {
        // Now create the product object with the valid ID
        $product = new WC_Product( $product_id );
        echo $product->get_price_html();
    } else {
        // Handle the case where no product was found
        echo 'No product found for this SKU.';
    }
}

If your custom field is not standard WooCommerce meta but rather an ACF field, you need to know the specific meta key associated with it. Often, ACF fields map to a specific meta_key in WordPress. If you don't have that mapping, using the $wpdb->prefix . 'postmeta' table directly allows you to search by the custom field name if it was registered as standard post meta.

Using WP_Query for Cleaner Code

If you prefer not to write raw SQL and want to stick to WordPress Object Relational Mapping (ORM) methods, WP_Query can be used, though it requires setting up parameters correctly.

global $woocommerce;
$sku = get_field( 'product_sku' );

if ( ! empty( $sku ) ) {
    // Check if this is a standard WooCommerce meta or ACF
    // Assuming 'product_sku' is the meta_key registered in wp_postmeta
    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 1,
        'meta_key'       => 'product_sku', // Adjust to your actual custom field key
        'meta_value'     => $sku,
        'orderby'        => 'ID',
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        $product_id = $query->posts[0]->ID;
        $product = new WC_Product( $product_id );
        
        echo $product->get_price_html();
    } else {
        // Product not found
    }
}

Trade-offs and Performance Considerations

When choosing between raw SQL and WP_Query, consider the performance implications. Your template page loads, potentially rendering many products dynamically. Running a WP_Query for every product lookup can be expensive if not cached. Raw SQL queries with proper prepared statements are generally faster for single lookups because they bypass the WordPress query layer overhead.

However, raw SQL requires you to handle escaping manually (via $wpdb->prepare), which increases the risk of SQL injection if done incorrectly. Always use placeholders (%s, %d) as shown in the examples above.

Another pitfall is handling duplicate SKUs. WooCommerce allows multiple products to share the same SKU unless configured otherwise. Your query should ideally order by ID and limit results to one, or check how your specific SKU settings are configured in the WooCommerce admin under Settings > Products. If duplicates exist, returning just the first match might serve your immediate needs, but be aware that $product->get_price_html() will reflect only that specific product instance.

If you are building a complex custom template system where this lookup happens frequently, consider caching the result. You could store the mapping between your custom field key and the product ID in Redis or WordPress Transients to avoid hitting the database repeatedly on every page load. For most standard implementations, however, a direct query is sufficient and keeps the logic transparent within your loop.

Our network