Last active
October 19, 2021 08:16
-
-
Save MjHead/ad86f6e8c3c658ee69f2cfa91f852e5a to your computer and use it in GitHub Desktop.
Allow to use terms with Jet Engine Data Stores
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 | |
/** | |
* 1. Add this code into the end of functions.php of your active theme without opening PHP tag; | |
* 2. Replace strings, metioned in the comments below, with your actual data. | |
*/ | |
add_filter( 'jet-engine/data-stores/store-post-id', function( $post_id, $store ) { | |
/** | |
* Replace 'cookies-store' with your actual Data Store slug | |
*/ | |
if ( 'cookies-store' !== $store->get_slug() ) { | |
return $post_id; | |
} | |
$current_object = jet_engine()->listings->data->get_current_object(); | |
if ( ! $current_object ) { | |
return $post_id; | |
} | |
$class = get_class( $current_object ); | |
switch ( $class ) { | |
case 'WP_Term': | |
return $current_object->term_id; | |
case 'WP_Post': | |
/** | |
* Replace 'category' with required taxonomy slug | |
*/ | |
$terms = wp_get_post_terms( $current_object->ID, 'category', array( 'fields' => 'ids' ) ); | |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { | |
return $terms[0]; | |
} else { | |
return $post_id; | |
} | |
default: | |
/** | |
* In other cases by default we can't determine term ID for sure so just return initial value | |
*/ | |
return $post_id; | |
} | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment