Created
August 28, 2023 14:55
-
-
Save jeremysimmons/18bd9430f57ca0f544f816116a8262b3 to your computer and use it in GitHub Desktop.
Get the first parent id of a simple product woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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