Need fresh eyes on a short PHP function that doesn't work, please
-
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)
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.