I need help setting up a post view counter

[ad_1]

Hi @quangtuyen1601,

How are you establishing the parent-child relationship between the “post” and the “chap” exactly?

Thank you for responding,
These are the codes I compiled online and asked chatgpt to build them.
Basically, a chapter is just a custom post, I don’t really understand how to define it.
For example, when I call the post’s chapter list, the query is like this
$post_id = get_the_ID(); $query_args = array( 'post_type' => 'chap', 'post_parent' => $post_id, )
Before using your plugin, I used code like this
// function to count views. function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } }
and in single-chap.php
$parent = $post->post_parent; $ID = get_the_ID(); tw_views($parent);
then it will count the views of the chapters for the post

I asked chatgpt and there is this way
$chap_id = get_the_ID();

$post_id = wp_get_post_parent_id($chap_id);
to determine post ID from chap ID.
Does your plugin support what I want?
Use the post ID placed in the chap to count views for the post

Does your plugin support what I want?

Yes, I’m just busy at the moment 😛

I’ll leave a comment here with a potential solution as soon as I can so you can try it out.

huhu

So is there no available function like the way I used to count views by post id?

I searched 49 help pages but did not find any case like mine. 😞

Hope you support back soon

Nope, I don’t think you will find a solution here that 100% fits your requirements. I don’t remember anyone asking here anything similar to what you want, and so you’ll have to wait for a bit until I’m free 😛

Thank you, I will be here waiting for you. Looking forward to your support

Alright, try adding this code snippet to your theme’s functions.php file:

/**
 * Updates parent's views count when visiting a "chap"
 */
add_action( 'wp_head', function() {
	if ( is_singular('chap') ) {
		$current_chap = get_queried_object();
		$parent_id = (int) $current_chap->post_parent;
		
		if ( $parent_id ) {
			?>
			<script>
				(function(){
					if ( 'undefined' !== typeof WordPressPopularPosts && 'undefined' !== typeof wpp_params ) {
						WordPressPopularPosts.post(
							wpp_params.api_url + '/v2/views/<?php echo $parent_id; ?>',
    						"_wpnonce=" + wpp_params.token + "&sampling=" + wpp_params.sampling_active + "&sampling_rate=" + wpp_params.sampling_rate,
							function( response ) {
								wpp_params.debug&&window.console&&window.console.log&&window.console.log(JSON.parse(response));
							}
						);
					}
				})();
			</script>
			<?php
		}
	}
}, 99);

If you’re using a page caching plugin (eg. WP Super Cache, WP Rocket, W3 Total Cache, etc) please make sure to clear its cache afterwards.

It was amazing, it worked very well.
Thank you very much.

Don’t mention it, glad I could help!

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer