Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function test_transients() {
- // Set the transient key
- $transient_key = 'my_products_transient';
- // Check if the transient exists
- if ( false === ( $products = get_transient( $transient_key ) ) ) {
- // If the transient does not exist, retrieve the products and store them in the transient
- $products = wc_get_products( array(
- 'status' => 'publish',
- 'limit' => 5,
- ) );
- // Set the transient with a 1-hour expiration time
- set_transient( $transient_key, $products, 60 * 60 );
- }
- // Now you can use the $products array, which may have been retrieved from the transient or queried and stored in it
- echo '<ul>';
- foreach( $products as $p ) {
- echo '<li>'. $p->get_name() . '</li>';
- }
- echo '</ul>';
- }
- add_action( 'init', 'test_transients' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement