Code Element Vc

Count WordPress Post Views using ACF

You can count post views in WordPress using Advanced Custom Field’s native update_field function.

Replace:

field_name: with the name of your view counter custom field

/**
 * Increment Post View Counter
 */
public static function countView()
{
    if (!is_user_logged_in()) {
        // Get the view count
        $count = (int) get_field('field_name');

        // Increment
        $count++;

        // Update with new count
        update_field('field_name', $count);
    }
}