| @@ -1,22 +1,11 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Query functions. | |
| 3 | + * Query functions | |
| 4 | 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 | 5 | * @package wpstream-theme |
| 10 | 6 | */ |
| 11 | 7 | |
| 12 | - | |
| 13 | -// Exit if accessed directly. | |
| 14 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | - exit; | |
| 16 | -} | |
| 17 | - | |
| 18 | -// Guard against redeclaration. | |
| 19 | 8 | if ( ! function_exists( 'wpstream_custom_query' ) ) { |
| 20 | 9 | /** |
| 21 | 10 | * Perform a custom query with optional transient caching. |
| 22 | 11 | * |
| @@ -31,9 +20,8 @@ | ||
| 31 | 20 | if ( $use_transient ) { |
| 32 | 21 | // Try to get the data from the transient cache. |
| 33 | 22 | $query = get_transient( $transient_key ); |
| 34 | 23 | |
| 35 | - // A cached WP_Query object was found: serve it directly. | |
| 36 | 24 | if ( false !== $query ) { |
| 37 | 25 | return $query; // Return cached query result if available. |
| 38 | 26 | } |
| 39 | 27 | } |
| @@ -38,28 +26,22 @@ | ||
| 38 | 26 | } |
| 39 | 27 | } |
| 40 | 28 | |
| 41 | 29 | // 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 | 30 | $query_args['ignore_sticky_posts'] = 1; |
| 44 | - // For non-search queries, exclude the current post from the results. | |
| 45 | 31 | if ( ! isset( $query_args['s'] ) ) { |
| 46 | 32 | $query_args['post__not_in'] = array( get_the_ID() ); |
| 47 | 33 | } |
| 48 | - // Run the query. | |
| 49 | 34 | $query = new WP_Query( $query_args ); |
| 50 | 35 | |
| 51 | 36 | // Check if the query has posts. |
| 52 | 37 | if ( $query->have_posts() ) { |
| 53 | - // Cache the populated result set for 6 hours when caching is enabled. | |
| 54 | 38 | if ( $use_transient ) { |
| 55 | 39 | set_transient( $transient_key, $query, 6 * 60 * 60 ); |
| 56 | 40 | } |
| 57 | 41 | } else { |
| 58 | - // No results: drop any stale cache entry for this key. | |
| 59 | 42 | delete_transient( $transient_key ); |
| 60 | 43 | } |
| 61 | - // Restore the global post after the secondary query. | |
| 62 | 44 | wp_reset_postdata(); |
| 63 | 45 | return $query; |
| 64 | 46 | } |
| 65 | 47 | } |