-
-
Save Lonsdale201/30ae5a3980c67f593b2ee81f18554e51 to your computer and use it in GitHub Desktop.
JetEngine - Data Store - Store item on view - Update custom meta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This code monitors a given data store type (Store item on view), | |
| // and updates and replaces it with the value stored in the store in a given meta (number based) programmed way. | |
| // It's ideal if you want to count the number of times a post has been viewed, and want to continuously transfer the value to a meta | |
| // (so you can do a popular sorting filter, for example) | |
| // place the code in the child theme functions.php or a custom code snippets plugin like FluentSnippets | |
| // how to use | |
| // Create a new Data store - enable the "Count Items", and "Store item" on view option, and choose post type | |
| // Create a number type meta (jetengine, or other plugins, or php) | |
| // In the Number meta, recommended to set a default 0 value | |
| // replace the "megtekintes" with your data store SLUG (you need to replace in two places top of the code, and bottom of the code !!) | |
| // replace the "ev_megtekintes" with your number meta ID | |
| // If you want to know what happening, dont delete the logs, this always will transfer information in your debug.log (if enabled) but you can delete all log if not need | |
| function update_post_view_count($item_id, $store_slug, $store_factory) { | |
| if ($store_slug === 'megtekintes') { | |
| $store_type = $store_factory->get_type(); | |
| if ($store_type) { | |
| $store_count = $store_factory->get_post_count($item_id); | |
| // Log the current count of items in the Data Store | |
| error_log("Data Store item count: $store_count, Post ID: $item_id"); | |
| $view_count = get_post_meta($item_id, 'ev_megtekintes', true); | |
| if ($view_count === '') { | |
| $view_count = 0; | |
| } | |
| // Ensure that the meta field value is an integer | |
| $view_count = intval($view_count); | |
| // Log the current value of the meta field | |
| error_log("Current meta field value: $view_count, Post ID: $item_id"); | |
| if ($view_count != $store_count) { | |
| $view_count = intval($store_count); | |
| update_post_meta($item_id, 'ev_megtekintes', $view_count); | |
| // Log the updated value of the meta field | |
| error_log("Meta field updated: Post ID: $item_id, New value: $view_count"); | |
| } | |
| } else { | |
| error_log("Store Type not found: $store_slug"); | |
| } | |
| } else { | |
| error_log("Invalid Data Store slug: $store_slug"); | |
| } | |
| } | |
| add_action('init', function() { | |
| if (jet_engine()->modules->is_module_active('data-stores')) { | |
| add_action('jet-engine/data-stores/after-add-to-store', 'update_post_view_count', 10, 3); | |
| add_action('jet-engine/data-stores/after-remove-from-store', 'update_post_view_count', 10, 3); | |
| } else { | |
| error_log("Data Store module not active."); | |
| } | |
| }, 1); | |
| add_action('template_redirect', function() { | |
| if (is_singular('post')) { | |
| $post_id = get_the_ID(); | |
| if (jet_engine()->modules->is_module_active('data-stores')) { | |
| $store_slug = 'megtekintes'; | |
| $store_factory = \Jet_Engine\Modules\Data_Stores\Module::instance()->stores->get_store($store_slug); | |
| update_post_view_count($post_id, $store_slug, $store_factory); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment