Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ben-heath/4a1c5aaedbb84ef56a83a52bd050e308 to your computer and use it in GitHub Desktop.
Save ben-heath/4a1c5aaedbb84ef56a83a52bd050e308 to your computer and use it in GitHub Desktop.
Restricting Display Posts shortcode posts to only show posts by Authors with specific WooCommerce Subscription
<?php
// This is dependant on Display Posts Shortcode plugin - https://wordpress.org/plugins/display-posts-shortcode/
// Alst dependant on WooCommerce Subscriptions Plugin - https://woocommerce.com/products/woocommerce-subscriptions/
// Adding the Author Subscription Level filter to Display Posts shortcode
function be_display_posts_shortcode_exclude_posts( $args, $atts ) {
if( isset( $atts['platinum'] ) ) {
$blogusers = get_users(); // get all users
$platinumUsers = array();
// Loop through users
foreach ( $blogusers as $user ) {
// testing if user has a specific subscription
if(wcs_user_has_subscription( $user->ID, 167, '' )){
// if they have the subscription, add them to the array
$platinumUsers[] = $user->ID;
}
}
// shows posts only if author is in the new array
$args['author__in'] = $platinumUsers;
}
return $args;
}
add_filter( 'display_posts_shortcode_args', 'be_display_posts_shortcode_exclude_posts', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment