Display Post Views & CPT Count with Crocoblock Jetengine | Elementor | WordPress
HTML-код
- Опубликовано: 23 авг 2024
- You might have face problem displaying Post Views for the CPT especially if your website is a newspaper, property listing or classified. In this tutorial, i will show you how to create it with few lines of code in JetEngine Crocoblock.
In addition to that it covers how to show total number of CPT.
Code to be inserted in Child theme:
$meta_values = get_post_meta( get_the_ID(), "posts-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'posts-count', $v2);
Download Elementor:
elementor.com/...
Download Crocoblock:
crocoblock.com...
Need to build dynamic websites?
softemblems.com
Follow me on Social Media
Facebook : / mxtchamkani
Instagram : / moxet
Twitter : / sokaniwaal
Track: Peruvian Woods - Dave Osorio [Audio Library Release]
Music provided by Audio Library Plus
Watch: • Peruvian Woods - Dave Osorio | Free B...
Free Download / Stream: alplus.io/peru...
Finding your channel made my day. I have been looking hard for someone who explains JetEngine more clearly than the docs on their website. I have been using Crocoblock for the past 4 years but I've only scratched the surface of what is possible I'm sure. That you are explaining some PHP code that interacts with it as well is also a big help. Thanks Liked! Subscribed!
Thank you for your interest and visting the channel, will try my best to share more valuable tutorials in future :)
Keep in touch please
Your channel and content!! Thank you for sharing this, you don't understand how beneficial it has been! Blessings to you.
Excellent video!!
I don't know what I'm doing wrong but you wouldn't be telling me..
I have put the code from the plugins code snippet
$meta_values = get_post_meta( get_the_ID(), "counter-properties", true);
$v2 = (int)$meta_values +1;
update_post_meta( get_the_ID(), "counter-properties", $v2);
Thanks for your time!!
please encapsulate your code in a filter as below
add_filter('the_content','view_counter');
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
Thanks Abdul Moxet, that's an amazing video. Looking forward to seeing the full video of how you built the all site.
First of all thank you for this video
But there are two problems when I use this code
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
add_filter('the_content','view_counter');
First one is the pages content disappeared.
I solved this by adding this line
return $content ;
The Second problem that the code works on edit post type page but doesn't work on a single page
The single page that I use is Crocoblock > Theme Builder > Single page
If any help I will be appreciate thanks
same here. doesnt work on a single page/single post.
@@thomasanderson5873
// Erhöhe den Seitenaufrufzähler
function increment_page_views() {
$current_views = get_post_meta(get_the_ID(), 'page_views', true);
if ($current_views == '') {
$current_views = 0;
}
$new_views = $current_views + 1;
update_post_meta(get_the_ID(), 'page_views', $new_views);
}
add_action('template_redirect', 'increment_page_views');
Views count worked for me.... Thanks
This is great information! I was already tracking this data from Google Analytics, but it can be attractive for high hit content for advertisers to access this data on my website :) Thank you
Hola,, que excelente video,, te agradezco por compartir tu conocimiento,, gracias y sigue adelante,, te encontré por casualidad y no sabes el gusto que me da,, nuevamente gracias y por favor, sigue haciendo este tipo de videos que son diferentes a los que se encuentran en RUclips
Muchas gracias por tu interés. Por favor, manténgase en contacto para más videos.
Moxet Khan, you are Gem...........
thanks for such good content...
Please make videos as soon as possible....
Great, I hope you continue to develop content about Jetengine
Hello, is there a way to trigger the counter with a time delay, so that it is prevented that a visitor drives the counter up by constantly loading the page. Example: The page is updated several times in one minute but the counter only counts up every minute or every 2 minutes.
Yes it is possible with the combination of cron job/delay and user agent, so the user agent will get the user details and cronjob will make sure it execute after X time. wordpress.stackexchange.com/questions/56266/delay-function-on-publish
Works great, thanks;
does not work with caching plugin, possible to do the same thing in AJAX or JS? Thanks
Hello muqsit, how can I make only signle views for single user. 1 view = 1 user. I think this is done via user meta query.
Hello, you are amazing.
by the way, do you know how to add numbers (numbering) to the posts yo my listing grid? and could you tell me how please?
for example, in my Listing, I want it looks like this:
Number 1*, Title Post, then the descriptions
Number 2*, Title Post, then the descriptions
Number 3*, Title Post, then the descriptions
etc...
* the numbers mean, it's automatically added, and ascending by date or name. according to the number of posts that appear on the page/archive.
thank you.
Hello, you have to use query builder to create a query which fetches all your post details in addition to one extra column for count. I have attached a sample of query builder here.
prnt.sc/_yWsUzdrQJ13
Once the query is done, you can display the columns in listing grid including counter column.
Excellent!! Thanks for sharing your knowledge!
Query:
*Is it possible to make a listing impression counter?
Most probabaly with jquery, the visible part of the canvas should send +1 to the impression meta in the back-end each time someone scroll. Its just an idea not yet implemented.
@@MoxetKhanPK it would be great
Thank you so much for solution. You have solution for display post view for CCT?
Anyone who faces a problem due to a recent update, please encapsulate your code in a filter as below
function increment_post_views() {
if (is_single()) {
$post_id = get_the_ID();
$views = get_post_meta($post_id, 'post-count', true);
// If the 'post_views' meta field doesn't exist, initialize it to 1
if (empty($views)) {
$views = 1;
} else {
$views = intval($views) + 1;
}
// Update the 'post_views' meta field with the incremented value
update_post_meta($post_id, 'post-count', $views);
}
}
add_action('wp', 'increment_post_views');
Hey man really love this! Could you please help me with my Rank?
I want to be able to view the posts that the most people clicked "submit" on.
So i got a elementor form with a submit button on my single-post page.
Everytime someone submits the form on a specific post, its "rank" counter should increase.
thanks
@@alwinlangmann89 Hello Alwin, thank you for you interest. Its easy, what you need to do is to have the button connect to an internal hook as notification in the form. When you click on button it should trigger the hook and the hook should have something like this [make sure your hook name is send]
add_action('jet-engine-booking/send', 'send', 10, 2);
function send($data, $id){
$post_id = $data["post_id"];
$meta_values = get_post_meta($post_id, "rank-meta", true);
$v2 = (int)$meta_values + 1;
update_post_meta($post_id, 'rank-meta', updated value);
}
@@MoxetKhanPK thank you so much! AWESOME
Hi, thank you for providing very useful information.
I post the vimeo URL to show the video in the post. After setting this filtered code , the page cannot load the vimeo. Is there any way to resoleve this??
And using the past code written in a description , the count function was not working.
I am looking for something like this, you helped me a lot. I am making a Directory site, where every business can see "How many times their profile is visited" in their Dashboard. Also, how many leads they collected (on the profile page there will be some offers if anyone "Claims" the offer by clicking the button, it will be counted as a lead). How to achieve that.?
In the dashboard, every user can see their own statistics (Profile visits & Collected leads). Hopefully you will help me with this.
Thanks for this video Bro, Congrats!
I'd like to add that for each post of a logged user. For example: a user visits a post and I want to tell him: "You've seen this post 4 times". It's for a college content review post type...
Hello, use JetEngine data store for that, it counts for logged in users.
@@MoxetKhanPK thank you, I'll check that up!
Hi Moxet the guru of Croco. Just asked for a tut about drop downs where in front end firm submission, a SELECT field will then restrict the options in the next select field, eg country, then city then suburb, that sort of thing PLEASE do a tut on that?? Trawled RUclips and found nothing at all.
Hello Jennifer, the issue is discussed like 32 weeks ago and mentioned by crocoblock that it is in pipeline to be released. There is no simple shortcut as of now but doable with jquery
stackoverflow.com/questions/30346640/javascript-jquery-dependent-drop-down-list
☹️ jquery… Perhaps do a tut in this??
Re the map listings function:
it seems a crucial thing is for the user to have a default “home” starting point, either from browser or from meta, but also the ability to filter the map, using cities, towns suburbs etc. Perhaps via a CCT with all the geolocations user could reference?
Then the search for coffee shops mechanics whatever which is already easily done with Jetengine. So three options for that important starter map - 1) the geolocate of the browser - done, 2) the meta of the USER address (switch?) and 3) Heirachical selects of country city suburb .. sort of thing?
Feels to me all of this is already possible, but my knowledge is to scant. Tut??
it not possible to use a dynamic range filter on the page to increase / decrease the results one finds? Also if the USER sets up his home suburb, which is geolocated in a CCT, then can we use that as the start place of the map on the page? All sounds (vaguely) doable already?? Please do more on this?
Its so awesome..thanks🙏
Hello there, Great Tutorial!! Many Thanks!! In my case the post counter rises up by 2 instead of 1 view every time i refresh the page. Any Tip how to resolve that? Thanks in advance!! Greatings from Greece!!
Hello Thank you for your interest, not sure what is wrong exactly but you can install code snippet and insert over there.
This was Brilliant. I am looking for the code of displaying the number of times a document (PDF) has been downloaded to be displayed on the front end
Thank you for the video. Yes I would love to see how you build the whole system from scratch. Sometimes to better understand some features, you have to explain what environment you are using it for. Waiting for the tutorial as you promised. Thank you again. This is awesome. But the way why didn't you just use the CPT or post count from Jetengine without using the codes in the child theme?
I will surely share the full case study soon, do we have built post views built in jetengine?
@@MoxetKhanPK waiting you build the whole system from scratch
thanks the code works, but my problem is that the counter is updated 2 times.
Example:
count_start = 116
count_end = 117
If at this moment you look at the value in the admin panel, then there will be 118.
I will be glad if someone can help me to solve the problem
Hi, need to check it thoroughly.
Please contact via email.
Nice Sir ... Thank You
Hey Abdul Moxet. Thank you for the great video, love your contents. I have a question; is it possible to do it for custom content types? I created a messaging system similiar to whatsapp by your videos. Basically, I created a post for chat and cct for messages. I want to inform users for not read messages.
Hi Moxet! Thank you for great video. What would be the code if I wanted to exclude admin views?
Please check this link
developer.wordpress.org/reference/functions/is_admin/
Hello and thank you for this amazing video!
IS there a way to implement this for button click tracking? I have a CPT called Therapists, and contact buttons (Call, Facebook, Mail etc), and the client wishes to count the clicks on this buttons individually for every therapist.
Is that possible?
I wold like to see this as well.
Thanks for receipe. How about caching plugin? Will be This works?
Not sure, need to test it.
Great tutorial, thanks! What about if we want to count viewers by session, not unique views?
So someone refresh the page 5 times but it counts 1?
@@MoxetKhanPK yes
I sort of have this working.
The main problem is that the counter is added to if I refresh the post in the wordpress dashboard as well, rather than just on the front end
Run the code in the frontend only. Use code snippet plugin.
I use custom post type. And create the views meta field there. I tried the code but it doesn't work.
Do I have to use metabox and create meta field?
Make sure you placed the code in functions.php and the meta value is visible in CPT
Hi, thanks for video. I use post grid from unlimited-elements, and I add custom fields. But when I click on post is post count to all posts. Please help :)
Hello, just turn off other plugins and themes and check it on blank setting. Make sure get_the_ID() function in place so it can add only in that post.
Hi Moxet, this video was really helpful thank you, but I am facing a issue the post views count does not increase when caching plugin is enabled..... When I disable caching for cpt single pages then everything works fine. Any idea on this??
Thanks!!
I'm using the Hello Elementor but don't have the child theme.. how to create the child theme?
this method not working when i test
Its an amazing video tutorial.. Please make a video how to build user profile ( Dashboard / login / sign up) by Crocoblock
you are the king
Hello Khan, This tutorial didn't work for me. I did exactly what I saw but the counter is somehow not working. Also, they all need to be updated one by one for the counter to appear on existing posts. It only seems to work on new posts. Where could I be doing wrong? Thank you
Two things to consider in your case. Make sure to paste the code inside qp_enque_style, 2nd thing i mentioned in tutorial that the default value of meta must be zero so it applies on both new and old posts.
@@MoxetKhanPK I used child theme by crocoblock, and it don't have qp_enque_style, so where shoud I put your code
/**
* Child functions and definitions.
*/
add_action( 'ocean_header', 'cb_child_do_header', -999 );
add_action( 'ocean_footer', 'cb_child_do_footer', -999 );
/**
* Apply Crocoblock custom headers
*
* @return [type] [description]
*/
function cb_child_do_header() {
cb_child_process_location( 'header', true );
}
/**
* Apply Crocoblock custom footers
*
* @return [type] [description]
*/
function cb_child_do_footer() {
cb_child_process_location( 'footer', true );
}
/**
* Process single location
*
* @return void
*/
function cb_child_process_location( $location = null, $cleanup = false ) {
if ( ! function_exists( 'jet_theme_core' ) ) {
return false;
}
if( ! defined( 'ELEMENTOR_VERSION' ) ) {
return false;
}
$done = jet_theme_core()->locations->do_location( $location );
if ( $done && $cleanup ) {
$hook = current_filter();
remove_all_actions( $hook );
}
return $done;
}
Thanks for your videos, valuable content indeed. In regard to this post count, is there a way to add a post view based on user role? I'd like to create a front end notification to tell the user how many posts are new or marked as view (meta field not equal to 0). Hope it makes sense.
You are just awesome
Always like your stuff, but I would like a counter for posts published by users based on user roles and CPTs i.e. I have cpt1, cpt2, cpt3, cpt4 and a user role is assigned to each CPT such as user-role1 publishes to cpt1, user-role2 publishes to cpt2, user-role3 publishes to cpt3, user-role4 publishes to cpt4. Each member at their levels would then know how many posts they have published.
You can create query for that, select all posts from cpt1 and display the query result count of that query to user-role1 only. same goes for the rest.
@@MoxetKhanPK Thank you, and what if I have 10 users with user-role1? Will it determine how many posts each user has?
@@Website-Developer yes it will determine
@@MoxetKhanPK So I am assuming the each user role would need to have its own template as one template can not be used for all user roles correct?
@@Website-Developer It can be used for multiple user role as well.
This needs to be refined. This doesn't work very well. I've tried multiple ways and every time it seems to be counting 3 views for every single view.
Plus, if you add the filter - the content disappears. Unless you return it using "return $content"
Thank you very much for the video.
By the way, how can I insert the code since I'm not using a child-theme ?
You can add it directly to your theme or using php snippet plugin which helps you add php code anywhere.
@@MoxetKhanPK thanks a lot !
How can we display most viewed most using jet listing?
The data of your views are stored in database, you can run a query on your post and sort by VIEWS [Meta field]
Hi Moxet, thank you for your contributions with these handy addons. I just tested this with the encapsulated code and followed exactly how you got this working in the video, but unfortunately my pages are loading blank w. that code in the functions.php child theme.
Any ideas if there are some setups that require a modification? Cheers!
Try using code snippets
great video, thanks!!! what i'd like to know is it possible to order a jetengine listing by number of most views (most popular posts) using this method?
Yes why not, goto query builder and fetch your data sort by most views descending.
Can you do a video on how to count posts for certain taxonomy and show the counts in menu
You dont need code for that, take dynamic field and choose term count, it shows number of posts in a category
Hola, Intente hacer conteo de visitas a la Single User Page que crea el Profile Builder pero no funciono.
Utilice tu Snippet pero con Get_user_meta y update_user_meta.
Ayuda por favor!
Please Make basic to advance tutorial on Jet Plugins
Great video. Question, what should I do if I don't want admin views to count? Thanks in advance
You can check from this answer whether the user is admin or not
wordpress.stackexchange.com/questions/5047/how-to-check-if-a-user-is-in-a-specific-role/54837#54837
good
May I know is it possible add the view count into Jet posts using your code in a filter as below?
How to make this counters when they go over 1000 to be 1k, and if in the case 1 Million to be 1M or even say 1.5k and etc?
In the output you have to apply this javascript code
stackoverflow.com/questions/10599933/convert-long-number-into-abbreviated-string-in-javascript-with-a-special-shortn
I did the same way how you did, but somehow not working for me.
Add this:
function increment_post_views() {
if (is_single()) {
$post_id = get_the_ID();
$views = get_post_meta($post_id, 'post-count', true);
// If the 'post_views' meta field doesn't exist, initialize it to 1
if (empty($views)) {
$views = 1;
} else {
$views = intval($views) + 1;
}
// Update the 'post_views' meta field with the incremented value
update_post_meta($post_id, 'post-count', $views);
}
}
add_action('wp', 'increment_post_views');
@@giand9614 It works. But if used with the button (count click) What do I need to add?
how we can do that for woocommerce prodcuts count and sales to show how much was sold this product
Use query builder instead, run a query which count products in orders table.
can you guide me to do that ?
i want to make default sales value and any one buy it the count will be improve it
@@MoxetKhanPK
Can I somehow make a custom field that will display whether the user is online or not?
Well it depends on environment; what mechanism you ll use to determine someone is online or offline, plugin might help. Manual will take timr
Thanks you broo
For some reason it doesn't work for me. I am trying to use the code snippets plugin to add the code, but I tried inserting it directly to the theme file editor, and still not working :(
I've created a metabox, assigned that to the CPT needed etc.
Did you tried to make meta boxes inside CPT instead of creating individual meta
@@MoxetKhanPK Yes. The first time I’ve just added a meta field to the CPT but the 2nd time, I’ve created a metabox and assign it to the CPT as you show. I reached to the support but they said that on the newest version might not work.
Does it still work on your website?
@@simeon.benevrekov i ll try it with latest version of jetengine.
please encapsulate your code in a filter as below
add_filter('the_content','view_counter');
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
Dont work in single page :(
Thank you.
Can you please make a video for Jet engine, about how to display multi color for product if we use Listing grid. I try to add colors to my product. Only colors.
I can use color picker, but it show only one, i try to shows more then one
I really hope you can help us with it.
Thank you again
Can you please eleborate more, how you will use those colors?
Can you please eleborate more, how you will use those colors?
If it's possible to reset the meta field 'posts-count' each 30 days to 0?
Yes possible, you have to setup a cron job which throw an action after every 30 days. The cron job PHP will have the meta keys map with the table you want to reset.
Isn't adding 1 to view supposed to be when viewing a ticket? If you just put your code in a function, then the addition will occur when you open any page and not a specific ticket. You need to do this in function.php add cheking:
if ( is_singular('ticket') ) {
$meta_values = get_post_meta( get_the_ID(), "posts-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'posts-count', $v2);
}
maybe Im wrong
please encapsulate your code in a filter as below
add_filter('the_content','view_counter');
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
@@MoxetKhanPK in this case function view_counter will be run befor showing every records.... Why not add a function call shortcode in single CPT template "ticket" ?
@@ProkomputerRup it totaly depends on your requirement and the way it should execute be it in footer, header, single page or home page, for logged in for guest and so on.
2024 no funciona :( Lo hecho tal como explicas en tu video
Unfortunately it didn't work for me. I successfully added the post count meta field and I can see it when posting or editing a custom post.
I copied the code on function.php file. The problem is my view count is stuck at zero. Nothing happens . I have watch your video multiple times step by step but for some reasons it doesn't work for me.
please encapsulate your code in a filter as below
add_filter('the_content','view_counter');
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
@@MoxetKhanPK Thank you, now is working. :)
This is amazing! Could you explain how I could reset that counter every 24 hours? I have a WooCommerce shop that I'd like to display how many people have explored this product in the last 24 hours, and then have it reset after the day ends.
You can reset meta values throgh cron job
I don't get it. I made Meta box for my posts, made meta field with exact posts-count Name/ID, added function to the child theme functions.php but the count value won't go up when refreshing the post.
Is there any cache etc plugin installed? Make sure to change the theme temporaly and apply the changes. I did the exact same thing in tutorial and it works.
@@MoxetKhanPK There was, tried without caching but couldn't get it to work.
@@MoxetKhanPK Tried it on fresh local WordPress installation and can't get it to work. Tried with normal posts and made a test CPT, doesn't update the value when visiting the page.
@@nikoweb Please DM me on facebook to resolve it, i saw your comments on facebook community as well.
please encapsulate your code in a filter as below
add_filter('the_content','view_counter');
function view_counter($content) {
$meta_values = get_post_meta( get_the_ID(), "post-count", true);
$v2 = (int)$meta_values + 1;
update_post_meta( get_the_ID(), 'post-count', $v2);
}
I have no child theme what can I do now?
Add directly in function.php or install code snippet plugin
Brother Please create different playlists for relevant videos, and add that in playlists. So new commers to the channel or anyone can easily find the Videos which they want to watch
how to diplay count of current user only
You can use data store for that, it counts in user meta.
@@MoxetKhanPK couldn't find thanks anyway
Kar ko allah de kamyab ka
Loving your videos!
Since you know your way around JetEngine, I have a quick question I hope you can answer. Is it possible to display user data (using data stores) in a dynamic chart against a meta field?
For example, I have a library with three states (reading, planned, completed) = data stores. Historical books are separated into different centuries (1st, 2nd, 3rd) = meta field - radio field. The problem is, I can’t figure out how to display the data in a dynamic chart where my users can see their progress broken by century.
Is this not possible or perhaps another way of using JetEngine to achieve this?
Been looking for an answer for this....
Next video pls
Can we have user views
Yup! We can have the same in user meta but we need to make sure the time script load is not in CPT but user instance.