I am desperately trying to programmatically get the post ID associated with a WooCommerce Subscription object. I am starting from the user id, and trying to request the database using the get_posts function. A call using the get_users_subscriptions
works but the objects returned do not include their ID
, only the associated order_id
or product_id
.
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions(
$current_user->ID
);
$subscription_posts = get_posts( array(
'orderby' => 'date',
'order' => 'ASC',
'numberposts' => 1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_subscription'
) );
The get_post
request is sadly returning an empty array. Do you see something wrong in my request ?
Thanks in advance,
I did not find an easy solution so I went with a SQL request based on the order_id contained in the subscription object I get using the call of:
The $wpdb call looks like the following:
If it helps someone, you’re welcome !
I’ve achieved the same thing with the following code, if you are using the wp rest APIs, it will also add a property to the response: