Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created November 9, 2021 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MjHead/8193573a1b4d72c258bbf66b80baf77d to your computer and use it in GitHub Desktop.
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
<?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 );
@moxet
Copy link

moxet commented Nov 11, 2021

Thank you, highly appreciate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment