The issue is coming from this code related to the shortcode implementation
// main item?
if ( ! in_the_loop() ) {
// get current object
$object = get_queried_object();
// post?
if ( is_a( $object, 'WP_Post' ) ) {
$defaults['id'] = $object->ID;
$defaults['type'] = 'post';
// term?
} elseif ( is_a( $object, 'WP_Term' ) ) {
$defaults['id'] = $object->term_id;
$defaults['type'] = 'term';
// user?
} elseif ( is_a( $object, 'WP_User' ) ) {
$defaults['id'] = $object->ID;
$defaults['type'] = 'user';
}
}
When using the shortcode on pages that are not archive ones or on places where we don’t have loops, the correct Post ID gets overridden by $object = get_queried_object();
and $defaults['id'] = $object->ID;
. This is causing all post counts across the page to have the same count number as the queried post – the one that the user is currently on.
You need to fix the code after // main item?
part.
The temporary workaround for all that have the same issue would be to use the pvc_post_views_html
filter like this:
add_filter( 'pvc_post_views_html', 'custom_pvc_post_views_html', 10, 5 );
/**
* It replaces the number of views in the Post Views Counter plugin with the number of views in the
* Post Views plugin
*
* @param html The HTML to be filtered.
*
* @return the html.
*/
function custom_pvc_post_views_html( $html ) {
if ( ! function_exists( 'pvc_get_post_views' ) ) {
return $html;
}
$views = pvc_get_post_views();
$html = preg_replace( '/<span class="post-views-count">(.*?)<\/span>/', '<span class="post-views-count">' . $views . '</span>', $html );
return $html;
}
The alternative option would be to use the pvc_get_post_views
function and pass a post ID to it