1. Customize
  2. Custom Functionalities
  3. [gamipress_earners_count] shortcode

[gamipress_earners_count] shortcode

The following snippet shows how to add a custom shortcode to show the number of times an achievement or rank has been earned.

Shortcode attributes:

  • id: Desired achievement ID or rank ID to count

Show the number of times earned of the achievement with ID 29:
[gamipress_earners_count id="29"]
Show the number of times earned of the rank with ID 167:
[gamipress_earners_count id="167"]


function my_prefix_gamipress_earners_count_shortcode( $atts ) {

    global $wpdb;

    $atts = shortcode_atts( array(
        'id' => '',
    ), $atts, 'gamipress_earners_count' );

    $id = absint( $atts['id'] );

    $user_earnings = GamiPress()->db->user_earnings;

    // Return the sum of achievements earned of all or specific points types
    return absint( $wpdb->get_var( "SELECT COUNT(*) FROM {$user_earnings} AS ue WHERE ue.post_id = {$id}" ) );

add_shortcode( 'gamipress_earners_count', 'my_prefix_gamipress_earners_count_shortcode' );

How can we help?