Problem: I would like to create a custom Page View counting system that increments a custom field in a User's profile when someone visits their content.
Solution: I can show you how to save some value in a User profile custom field using PHP, and how to retrieve that value from a User profile custom field using PHP.
Your code:
$meta = get_user_meta( $post->ID, 'wcpf-count', TRUE );
Is $post->ID accurate here? I think not. You need an author's User ID, not a post ID.
Example:
$meta = get_user_meta( 12345, 'wcpf-count', TRUE );
The variable $meta will contain the value of the wpcf-count field in the profile of User ID 12345. Replace 12345 with the correct User ID, or a variable representing the correct User ID. Try hard-coding a value first, then replace that with a variable and test again.
Your code:
update_user_meta( $authordata->ID, wcpf-count', implode(',', $meta) ); You have a syntax error here. The second parameter, wpcf-count, is not correctly surrounded by quotes. Example: [php] update_user_meta( 12345, 'wcpf-count', 'some value' );
Replace 12345 with the correct author's ID, and replace 'some-value' with the correct value to store in the custom field. Try hard-coding the values first, then once things work as expected replace the hard-coded values with variables.
How you trigger this code is up to you. That part of the application falls outside the scope of support we provide here in the forums.
Relevant Documentation:
https://codex.wordpress.org/Function_Reference/get_user_meta
https://codex.wordpress.org/Function_Reference/update_user_meta
https://toolset.com/documentation/customizing-sites-using-php/functions/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 3 replies, has 2 voices.
Last updated by 6 years, 10 months ago.
Assisted by: Christian Cox.