Hi there,
Thank you for contacting us and I'll be happy to assist.
To update the count of a custom field, when a link is clicked, you'll need to use WordPress' AJAX calls.
( ref: https://codex.wordpress.org/AJAX_in_Plugins )
Although 1-1 assistance around custom programming is beyond the scope of support that we provide, we do our best to guide in the right direction whenever possible.
The code that you'll need for this can be divided into two parts:
1. You'll need to add the code to load some custom scripts and AJAX action, in your active theme's "functions.php" file.
For example:
// load and localize custom scripts
add_action( 'wp_enqueue_scripts', 'ajax_counter_link_enqueue_scripts' );
function ajax_counter_link_enqueue_scripts() {
wp_enqueue_script( 'counter-link', get_theme_file_uri( '/js/link-counter.js' ), array('jquery'), '1.0', true );
wp_localize_script( 'counter-link', 'counterlink', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'ajax_nonce' => wp_create_nonce( 'link_click_counter_' . admin_url( 'admin-ajax.php' ) ),
));
}
// action to execute AJAX call
add_action( 'wp_ajax_nopriv_click_counter_process', 'click_counter_process' );
add_action( 'wp_ajax_click_counter_process', 'click_counter_process' );
function click_counter_process() {
// get current value of counter custom field
$current_count = get_post_meta( $_POST['post_id'], 'wpcf-link-counter', true );
// increment the counter value by 1
$current_count++;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX && wp_verify_nonce( $_POST['nonce'], 'link_click_counter_' . admin_url( 'admin-ajax.php' ) ) ) {
update_post_meta( $_POST['post_id'], 'wpcf-link-counter', $current_count );
echo "pass";
die();
} else {
die( 'Security check failed' );
}
}
Notes: The above code block assumes that the custom field slug for the counter custom field that you're using is "link-counter". But you should update all instances of "wpcf-link-counter" to match your custom field's slug.
2. Next, you can create a new file "link-counter.js" in a folder named "js" in your active theme's folder and include the following code in it, to perform the AJAX requests on click event:
jQuery( document ).on( 'click', '.click-counter-link', function(event) {
event.preventDefault();
var post_id = jQuery(this).data('id');
var post_URL = jQuery(this).attr('href');
jQuery.ajax({
url : counterlink.ajax_url,
type : 'post',
data : {
action : 'click_counter_process',
nonce : counterlink.ajax_nonce,
post_id : post_id
},
success : function( response ) {
if (response == "pass") {
window.location.href = post_URL;
}
else
{
alert(response);
}
}
});
})
After both these steps have been completed, you can add the actual links for each of your custom posts in the view like this:
<a class="click-counter-link" href="[wpv-post-url]" data-id="[wpv-post-id]">Click</a>
As a result, when visitors will click this link for a post through this view, they'll be redirected to that post and counter for the custom field value will be incremented by 1.
For a more personalize assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
I hope this helps.
regards,
Waqar