Skip to content

Instantly share code, notes, and snippets.

@jeremysimmons
Created August 28, 2023 14:55
Show Gist options
  • Save jeremysimmons/18bd9430f57ca0f544f816116a8262b3 to your computer and use it in GitHub Desktop.
Save jeremysimmons/18bd9430f57ca0f544f816116a8262b3 to your computer and use it in GitHub Desktop.
Get the first parent id of a simple product woocommerce
function wc_get_first_parent($prod_id) {
$group_args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_children',
'value' => 'i:' . $prod_id . ';',
'compare' => 'LIKE',
)
),
'fields' => 'ids' // THIS LINE FILTERS THE SELECT SQL
);
$parents = get_posts( $group_args );
return count($parents) > 0 ? array_shift($parents) : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment