Tagged: 

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #10009

    brandondurham
    Participant

    I’m using the WP REST API and a JS front-end to display posts. As I understand it that means a view won’t be registered since I’m not actually rendering anything in a view with WordPress. I’ve added several custom endpoints for the API in my functions.php file and can easily add a method to log a view when retrieving a post, but I’m not sure if this plugin allows for that, or has a public method for it?

    #10010

    Bartosz
    Keymaster

    Nice question @brandondurham,

    Actually we’re working on some improvements to make the plugin more flexible and enable a way to manually trigger different events. For now you can switch to JS mode in plugin settings and directly call Post_Views_Counter()->counter->check_post_ajax() method.

    There are a couple of required parameters:

    action – pvc-check-post
    post_id – integer, post id to count views
    pvc_nonce – pvc-check-post (wp nonce)
    post_type – current post type, must match post type of a given post_id

    This method is used to count visit via POST ajax request. And probably you could use a custom endpoint to trigger a call to the check_post_ajax() method.

    #10032

    brandondurham
    Participant

    Thanks for the quick response!

    Sorry, I’m a bit lost with the recommendation. This is an endpoint I have added in my theme’s functions.php file:

    function bbrew_article($data) {
        $query = apply_filters( 'bbrew_article', array(
    		'name' => $data['name'],
            'posts_per_page' => 1,
            'post_type' => 'post',
            'post_status' => 'publish'
        ));
        $all_posts = get_posts( $query );
        $return = array();
    
        foreach ( $all_posts as $post ) {
    		// Post Categories
    		$post_categories = wp_get_post_categories( $post->ID );
    		$cats = array();
    		foreach($post_categories as $c){
    			$cat = get_category( $c );
    			$cats[] = array(
    				'name' => $cat->name,
    				'slug' => $cat->slug,
    				'parent' => $cat->parent
    			);
    		}
    
    		// Image ID
    		$image_id = get_post_thumbnail_id( $post->ID );
    		// Image caption
    		$image_meta = get_post( $image_id );
    		// Custom fields
    		$custom_fields = get_post_meta( $post->ID, '', true );
    		// Custom taxonomy
    		$terms = get_the_terms( $post->ID, 'special-series' );
    
            $return = array(
    			'viewCount' => pvc_get_post_views( $post->ID ),
                'id' => $post->ID,
                'title' => $post->post_title,
    			'modified' => $post->post_modified,
    			'slug' => $post->post_name,
    			'url' => str_replace(home_url(), '', get_permalink($post->ID)),
    			'date' => $post->post_date,
    			'content' => apply_filters('the_content', $post->post_content),
    			'excerpt' => $post->post_excerpt,
    			'categories' => $cats,
    			'specialSeries' => $terms[0],
    			'commentCount' => intval($post->comment_count),
    			'author' => array(
    				'slug' => get_the_author_meta( 'user_nicename', $post->post_author ),
    				'name' => get_the_author_meta( 'display_name', $post->post_author )
    			),
    			'images' => array(
    				'thumbnail' => wp_get_attachment_image_src( $image_id ),
    				'bbrew-small' => wp_get_attachment_image_src( $image_id, 'bbrew-small' ),
    				'bbrew-medium' => wp_get_attachment_image_src( $image_id, 'bbrew-medium' ),
    				'bbrew-feature-small' => wp_get_attachment_image_src( $image_id, 'bbrew-feature-small' ),
    				'bbrew-feature' => wp_get_attachment_image_src( $image_id, 'bbrew-feature' )
    			),
    			'imageMeta' => array(
    				'date' => $image_meta->post_date,
    				'description' => $image_meta->post_content,
    				'title' => $image_meta->post_title
    			),
    			'customFields' => $custom_fields
            );
        }
    
    	// do_action( 'pvc_before_count_visit', $post->ID );
    	// Post_Views_Counter()->counter->check_post_ajax();
    
        $response = new WP_REST_Response( $return );
        $response->header( 'Access-Control-Allow-Origin', apply_filters( 'bbrew_access_control_allow_origin','*' ) );
        return $response;
    }

    How might I incorporate your suggestion to trigger a view?

    #10033

    Bartosz
    Keymaster

    Hey,

    Maybe I did not understand you. If you’re trying to get the post views along other post data in your custom endpoint (at least that’s how it looks in your code) then you have already done that:
    ‘viewCount’ => pvc_get_post_views( $post->ID ),

    It should work fine. Doesn’t it?

    #10034

    brandondurham
    Participant

    Sorry… I’m getting the view count just fine with pvc_get_post_views. You can see a screenshot of the JSON returned here: http://cloud.smallparade.com/eleY

    But I need to also log or trigger a view for the post when hitting this endpoint. Right now, since I’m retrieving the posts using the WP REST API, a view is not logged, so the view count for all posts always stays the same: “1”.

    #10037

    Bartosz
    Keymaster

    Hmmm… so I did understand you but gave you the wrong answer.

    If you want to do this in one request (as you do above) then this might work:

    Step 1:

    Go to plugin folder / includes / counter.php, find check_post() method and:

    remove this line from check_post() method:

    // get post id
    	$id = get_the_ID();

    and replace this:

    public function check_post() {

    with this:

    
    public function check_post( $id = 0 ) {
    	// get post id
    	$id = (int) (empty( $id ) ? get_the_ID() : $id);

    Step 2:

    Then it should be possible to count visit using this method:

    // count visit manually for a given post ID
    Post_Views_Counter()->counter->check_post( $post->ID );

    Keep in mind that PVC will check if user visit should be counted (as in plugin settings). If that works fine we’ll use that code in the upcoming release.

    #10038

    brandondurham
    Participant

    Sorry, no dice. No errors or anything, but also no change in the count.

    #10040

    Bartosz
    Keymaster

    Two questions:

    1. Do you have the count method set to PHP? (you will need that in that solution – there is a condition in check_post() method)

    2. Do you clear the post views cookie to check if the views is being counted – otherwise it will not count your visit.

    #10041

    brandondurham
    Participant

    1. I sure did. Thought the same thing, then went and changed it back to PHP but it still doesn’t count a view.

    2. Yeah, I actually cleared cookies and used private browsing mode.

    #10043

    Bartosz
    Keymaster

    I know what’s the problem.

    It would be easier if you contact us via contact form – i’ll send you the zipped counter.php file in reply.

    #10468

    colemast
    Participant

    This is the exact same issue I am having. Is it possible for me to get the counter.php file also, or code to add to either my functions or post endpoint file?

    #10470

    Bartosz
    Keymaster

    @colemast since v 1.2.0 there’s a public function available: pvc_view_post( $post_id ) I believe you could call that in your custom endpoint handler.

    #35758

    frzdegari
    Participant

    How to implement pvc_view_post($post_id) on flutter android app project?
    so my app is call the post on wordpress.com/wp-json/wp/v2/post/(id) when opening the post
    how can i update the pvc counter for the post = (id)

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.