Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Hector Cabrera

    (@hcabrera)

    Hi @quangtuyen1601,

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

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    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

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    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

    Plugin Author Hector Cabrera

    (@hcabrera)

    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.

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    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

    Plugin Author Hector Cabrera

    (@hcabrera)

    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 ??

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

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

    Plugin Author Hector Cabrera

    (@hcabrera)

    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.

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

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

    Plugin Author Hector Cabrera

    (@hcabrera)

    Don’t mention it, glad I could help!

    Thread Starter quangtuyen1601

    (@quangtuyen1601)

    hello @hcabrera, I got a 404 error for wpp_params.api_url + ‘/v2/views/’ and it doesn’t update the view from the chapter to the post anymore

    Plugin Author Hector Cabrera

    (@hcabrera)

    Hey @quangtuyen1601,

    Yes, there were some plugin changes that aren’t compatible with the code I shared before. Please use this one instead:

    /**
    * 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.apiUrl + '/v2/views/<?php echo $parent_id; ?>',
    "_wpnonce=" + wpp_params.token + "&sampling=" + wpp_params.sampling + "&sampling_rate=" + wpp_params.samplingRate,
    function( response ) {
    wpp_params.debug&&window.console&&window.console.log&&window.console.log(JSON.parse(response));
    }
    );
    }
    })();
    </script>
    <?php
    }
    }
    }, 99);
Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.