您可以使用以下功能
/**
* Retrieve a product given its slug.
*/
function get_product_by_slug($page_slug, $output = OBJECT) {
global $wpdb;
$product = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, \'product\'));
if ( $product )
return get_post($product, $output);
return null;
}
用法:
$product = get_product_by_slug(\'samsung-1\');
但它没有经过测试