Skip to content

Instantly share code, notes, and snippets.

@Alimir
Created March 18, 2021 12:41
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 Alimir/2557874c4e841fc7ecc6dcd3cf6a8cad to your computer and use it in GitHub Desktop.
Save Alimir/2557874c4e841fc7ecc6dcd3cf6a8cad to your computer and use it in GitHub Desktop.
wp ulike count total likes and dislikes
<?php
function wp_ulike_custom_count_logs( $atts ){
// Global variable
global $wpdb;
// Default Args
$args = shortcode_atts( array(
"status" => 'like',
"type" => 'post'
), $atts );
$table_name = "{$wpdb->prefix}ulike_meta";
$is_distinct = wp_ulike_setting_repo::isDistinct( $args['type'] );
$meta_key_name = sprintf( 'count_%s_%s', $is_distinct ? 'distinct' : 'total', $args['status'] );
$counter_sum = $wpdb->get_var(
$wpdb->prepare(
"SELECT SUM(`meta_value`) FROM {$table_name} WHERE `meta_key` = '%s' and `meta_group` = '%s'",
$meta_key_name,
$args['type']
)
);
return empty( $counter_sum ) ? 0 : (int) $counter_sum;
}
add_shortcode( 'wp_ulike_custom_count_logs', 'wp_ulike_custom_count_logs' );
@Alimir
Copy link
Author

Alimir commented Mar 18, 2021

Shorcode examples:
[wp_ulike_custom_count_logs]
[wp_ulike_custom_count_logs type="post" status="dislike"]
[wp_ulike_custom_count_logs type="comment" status="like"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment