Hi @todeswalzer,
I’m seeing the same behavior as you are. No idea what’s going on, to be honest.
Here’s a different workaround:
/**
* Appends a special parameter to the URL to
* prevent updating the views count of an article
* if referred from home.
*
*/
function my_custom_single_popular_post( $post_html, $p, $instance ){
// Return popular post link with "do not track this" parameter
if ( is_home() ) { // might need to change this condition with is_front_page()
$output = '<li><a href="' . get_permalink($p->id) . ( is_home() ? "?track=0" : "" ) . '" class="my-custom-title-class" title="' . esc_attr($p->title) . '">' . $p->title . '</a></li>';
} // Return the regular popular post link
else {
$output = '<li><a href="' . get_permalink($p->id) . '" class="my-custom-title-class" title="' . esc_attr($p->title) . '">' . $p->title . '</a></li>';
}
return $output;
}
add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
/**
* Exclude post type 'post' from WPP's tracking
* on demand.
*/
function my_trackable_post_types( $post_types ){
if ( isset( $_GET['track'] ) && 0 == $_GET['track'] ) {
$exclude_from_tracking = array( 'post' );
$post_types = array_diff( $post_types, $exclude_from_tracking );
}
print_r( $post_types );
return $post_types;
}
add_filter( 'wpp_trackable_post_types', 'my_trackable_post_types', 10, 1 );
Explanation:
We’re going to use two hooks: wpp_post and wpp_trackable_post_types. The former allows me to customize the HTML output of every popular posts, while the latter allows me to tell WPP which post types it should track.
In my_custom_single_popular_post() we check if the user is currently viewing the home page (check the code for details, you may need to adjust it) and if so then we append an arbitrary parameter to the URL of the post for later use (I chose track for this example but you can name it whatever you want).
When the user visits the post using this link, my_trackable_post_types() will check if the parameter we set before (track in my example) is present and if it’s 0 (you may want to tweak the conditions). If the conditions are met, it’ll ask WPP not to track this pageview by excluding the post type post from the array of post types WPP tracks by default (post, page and every public custom post type registered by your theme or a plugin, in case you’re wondering).
So, basically, with this we’re telling WPP:
“Hey, if the URL has X parameter and the post type is Y, don’t increment the views count.”
There might be a more elegant way of doing this but I’m pretty tired now (and it’s only Tuesday D:) and this is the best my brain can do at this moment 😛