Yith WooCommerce Subscriptions : List all the active subscribers

In order to do a custom listing of active subscriptions in Yith WooCommerce Subscription plugin, here is the code you can write:
Assuming your WordPress database table prefix is the default: “wp_”


function list_all_subscriptions() {

	global $wpdb;

	$query= "";
	$query .= "SELECT wp_posts.* FROM wp_posts ";
	$query .= "INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) ";
	$query .= "WHERE 1=1 ";
	$query .= "AND wp_posts.post_type = 'ywsbs_subscription' ";
	$query .= "GROUP BY wp_posts.ID ";
	$query .= "ORDER BY ID DESC";

	$subscribers = $wpdb->get_results( $query );
}

Hell, if you want to move one step further and list only active members, then you can throw “AND” condition in the query itself, like below;


function list_all_subscriptions() {

	global $wpdb;

	$query= "";
	$query .= "SELECT wp_posts.* FROM wp_posts ";
	$query .= "INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) ";
	$query .= "WHERE 1=1 ";
	$query .= "AND wp_posts.post_type = 'ywsbs_subscription' ";
	$query .= "AND ( wp_postmeta.meta_key = 'status' AND wp_postmeta.meta_value = 'active' ) "; 
        // will filters only active members
	$query .= "GROUP BY wp_posts.ID ";
	$query .= "ORDER BY ID DESC";

	$subscribers = $wpdb->get_results( $query );
}

You can do the pagination as well now.

Final Note: There is a file /plugins/yith-woocommerce-subscription/includes/admin/class.ywsbs-subscriptions-list-table.php, look or find the function prepare_items() , this is where I derived above query from.

Leave a Reply

Your email address will not be published. Required fields are marked *