• Resolved Ghostwise_

    (@ghostwise_)


    It is a simple block using an ACF custom field called “count” (or “field_5465eef2489a9” for its friends).

    When a post is displayed, if its count value is null it goes to 1. If it’s 1 or more, it is increased by one. The prefetching action also gets disabled so it doesn’t mess with this simple views counter.

    Currently the count cannot progress past “1”. I’ve run out of ideas. I don’t really speak PHP, so the problem might be absolutely trivial.

    Thanks in advance.

    /**
     * Count increaser
     * Increases the value of a new meta field field by one for each view
    */
    function ACF_count_tally($count, $post_id) {
    	$count = get_field('field_5465eef2489a9', $post_id);
    	if( $count ):
    		update_field('field_5465eef2489a9', $count++, $post_id);
    	else:
    		update_field('field_5465eef2489a9', 1, $post_id);
    	endif;
    }
    //To keep the count accurate, disable prefetching
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    
    //trigger for the count function
    function ACF_count_accrue ($post_id) {
        if ( !is_single() ) return;
        if ( empty ( $post_id) ) {
            global $post;
            $post_id = $post->ID;
        }
        ACF_count_tally($count, $post_id);
    }
    add_action( 'wp_head', 'ACF_count_accrue');
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Where do you set the field to 0?

    Moderator keesiemeijer

    (@keesiemeijer)

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this

    //To keep the count accurate, disable prefetching
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
    
    function ACF_count_tally( $post_id = 0 ) {
    
    	if ( !$post_id ) {
    		return;
    	}
    
    	$count = get_field( 'field_5465eef2489a9', $post_id );
    
    	// convert to integer
    	$count = absint( $count );
    
    	if ( $count ):
    		update_field( 'field_5465eef2489a9', ++$count, $post_id );
    	else:
    		update_field( 'field_5465eef2489a9', 1, $post_id );
    	endif;
    }
    
    //trigger for the count function
    function ACF_count_accrue() {
    
    	if ( !is_single() ) {
    		return;
    	}
    
    	ACF_count_tally( get_the_ID() );
    
    }
    add_action( 'wp', 'ACF_count_accrue' );

    Thread Starter Ghostwise_

    (@ghostwise_)

    I have deployed your proposed solution. I mean, Tintin would never lie to me, would he ? ??

    One brief test seems conclusive. Right now I am busy playing whack-a-mole with 404s (big site migration going) but that leaves time for views to accrue so I can see the behaviour of numbers in Count fields.

    Thanks muchly.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome ??

    Thread Starter Ghostwise_

    (@ghostwise_)

    As it turns out you’ve been even more helpful than I suspected.

    I was also using the plugin “Top 10” to keep counts (since my function wasn’t working, you see ?) and apparently it was causing a large performance hit on my site (due to my specific configuration with Varnish caching and whatnot).

    Since our function now works I uninstalled the plugin, apparently (fingers crossed!) solving the server load issue that was driving me bonkers.

    Victory !

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Need fresh eyes on a short PHP function that doesn't work, please’ is closed to new replies.