Created
November 9, 2021 12:57
-
-
Save MjHead/8193573a1b4d72c258bbf66b80baf77d to your computer and use it in GitHub Desktop.
JetEngine. Allows to update required CCT items on change posts in selected user meta data store
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
<?php | |
/** | |
* Allows to update required CCT items on change posts in selected user meta data store | |
*/ | |
add_filter( 'jet-engine/data-stores/ajax-store-fragments', function( $fragments, $store ) { | |
// Replace 'quoted-list' with your actual Data Store slug | |
$my_store = 'quoted-list'; | |
// Replace 'users_wishlist' with you actual CCT slug | |
$my_cct = 'users_wishlist'; | |
// Replace 'post_ids' with your actual field name for the posts from the CCT | |
$cct_posts_field = 'post_ids'; | |
// Replace 'user_id' with your actual field name for the user from the CCT | |
$cct_user_field = 'user_id'; | |
if ( $my_store === $store->get_slug() ) { | |
$posts = $store->get_store(); | |
$cct = \Jet_Engine\Modules\Custom_Content_Types\Module::instance()->manager->get_content_types( $my_cct ); | |
$user_id = get_current_user_id(); | |
if ( $cct ) { | |
$handler = $cct->get_item_handler(); | |
$exists = $cct->db->get_item( $user_id, $cct_user_field ); | |
$item_id = ! empty( $exists ) ? $exists['_ID'] : false; | |
$handler->update_item( array( | |
'_ID' => $item_id, | |
$cct_user_field => $user_id, | |
$cct_posts_field => $posts, | |
) ); | |
} | |
} | |
// Do not remove this code to keep data stores working | |
return $fragments; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, highly appreciate.