PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / trunk
WpStream – Live Streaming, Video on Demand, Pay Per View vtrunk
4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 4.5.12 All 180 releases
wpstream / hello-wpstream / framework / query-functions.php

query-functions.php in WpStream – Live Streaming, Video on Demand, Pay Per View trunk, at hello-wpstream/framework/query-functions.php

66 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Query functions.
4 *
5 * A thin wrapper around WP_Query that adds optional transient caching so
6 * repeated listing queries can be served from cache. The current post is
7 * excluded from non-search queries to avoid showing the page you are on.
8 *
9 * @package wpstream-theme
10 */
11
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 // Guard against redeclaration.
19 if ( ! function_exists( 'wpstream_custom_query' ) ) {
20 /**
21 * Perform a custom query with optional transient caching.
22 *
23 * @param array $query_args Query arguments.
24 * @param string $transient_key Transient key for caching.
25 * @param bool $use_transient Whether to use transient caching. Default is false.
26 *
27 * @return WP_Query|array|null The query result.
28 */
29 function wpstream_custom_query( $query_args, $transient_key, $use_transient = false ) {
30 // Check if transient caching is enabled and post type is not "post".
31 if ( $use_transient ) {
32 // Try to get the data from the transient cache.
33 $query = get_transient( $transient_key );
34
35 // A cached WP_Query object was found: serve it directly.
36 if ( false !== $query ) {
37 return $query; // Return cached query result if available.
38 }
39 }
40
41 // If not using transient caching or cache is not available, proceed with the query.
42 // Never let sticky posts jump the ordering of these listings.
43 $query_args['ignore_sticky_posts'] = 1;
44 // For non-search queries, exclude the current post from the results.
45 if ( ! isset( $query_args['s'] ) ) {
46 $query_args['post__not_in'] = array( get_the_ID() );
47 }
48 // Run the query.
49 $query = new WP_Query( $query_args );
50
51 // Check if the query has posts.
52 if ( $query->have_posts() ) {
53 // Cache the populated result set for 6 hours when caching is enabled.
54 if ( $use_transient ) {
55 set_transient( $transient_key, $query, 6 * 60 * 60 );
56 }
57 } else {
58 // No results: drop any stale cache entry for this key.
59 delete_transient( $transient_key );
60 }
61 // Restore the global post after the secondary query.
62 wp_reset_postdata();
63 return $query;
64 }
65 }
66