/**
* When a user's role is changed to Subscriber (by an admin or by the package-expiry code),
* every published listing / event of that user becomes a Draft.
* wp_update_post() is used (not a raw SQL UPDATE) so the listing index and other save hooks stay in sync.
*/
add_action('set_user_role', function ($user_id, $role, $old_roles) {
if ($role !== 'subscriber') return;
if (in_array('subscriber', (array)$old_roles, true)) return; // already a subscriber: nothing to do
$ids = get_posts(array(
'author' => (int)$user_id,
'post_type' => apply_filters('khads_subscriber_draft_post_types', array('ait-item', 'ait-event-pro')),
'post_status' => 'publish',
'numberposts' => -1,
'fields' => 'ids',
'suppress_filters' => true, // include every language (Polylang)
));
foreach ($ids as $pid) {
wp_update_post(array('ID' => (int)$pid, 'post_status' => 'draft'));
}
}, 20, 3);