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 gabrielmerovingi/9529817 to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/9529817 to your computer and use it in GitHub Desktop.
This is a simple example where we unpublish a users posts if their account goes minus 20 points.
/**
* Unpublish users Posts
* If a user reaches or exceeds the minimum balance
* we unpublish all their posts.
* @version 1.0
*/
add_filter( 'mycred_add', 'mycred_unpublish_posts_on_minus', 999, 3 );
function mycred_unpublish_posts_on_minus( $reply, $request, $mycred ) {
if ( $reply === false ) return;
// Get users current balance
$current_balance = $mycred->get_users_cred( $request['user_id'], $request['type'] );
// Max negative balance
$max = -20;
// Check balance
if ( $current_balance <= $max ) {
// User has reached the max, unpublish all their posts
global $wpdb;
// Change ALL posts to pending for this user.
$wpdb->query( $wpdb->prepare( "
UPDATE {$wpdb->posts}
SET post_status = %s
WHERE post_author = %d
AND post_status = %s;". 'pending', $request['user_id'], 'publish' ) );
}
// Always return a reply
return $reply;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment