bb_cache_posts

The BuddyPress bb cache posts function.

Description

bb_cache_posts( $query, (bool) $post_id_query = false ); 

Parameters (2)

0. $query
The query.
1. $post_id_query — Optional. (bool) => false
The post id query.

Usage

  1. if ( !function_exists( 'bb_cache_posts' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-posts.php'; 
  3.  
  4. // The query. 
  5. $query = null; 
  6.  
  7. // The post id query. 
  8. $post_id_query = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bb_cache_posts($query, $post_id_query); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-forums/bbpress/bb-includes/functions.bb-posts.php  
  1. function bb_cache_posts( $query, $post_id_query = false ) { 
  2. global $bbdb; 
  3.  
  4. $_query = ''; 
  5. $_query_post_ids = array(); 
  6. $_query_posts = array(); 
  7. $_cached_posts = array(); 
  8. $ordered_post_ids = array(); 
  9.  
  10. if ( $post_id_query && is_string( $query ) ) { 
  11. // The query is a SQL query to retrieve post_ids only 
  12. $key = md5( $query ); 
  13. if ( false === $post_ids = wp_cache_get( $key, 'bb_cache_posts_post_ids' ) ) { 
  14. if ( !$post_ids = (array) $bbdb->get_col( $query, 0 ) ) { 
  15. return array(); 
  16. wp_cache_add( $key, $post_ids, 'bb_cache_posts_post_ids' ); 
  17. $query = $post_ids; 
  18.  
  19. if ( is_array( $query ) ) { 
  20. $get_order_from_query = false; 
  21.  
  22. foreach ( $query as $_post_id ) { 
  23. $ordered_post_ids[] = $_post_id; 
  24. if ( false === $_post = wp_cache_get( $_post_id, 'bb_post' ) ) { 
  25. $_query_post_ids[] = $_post_id; 
  26. } else { 
  27. $_cached_posts[$_post->post_id] = $_post; 
  28.  
  29. if ( count( $_query_post_ids ) ) { 
  30. // Escape the ids for the SQL query 
  31. $_query_post_ids = $bbdb->escape_deep( $_query_post_ids ); 
  32.  
  33. // Sort the ids so the MySQL will more consistently cache the query 
  34. sort( $_query_post_ids ); 
  35.  
  36. $_query = "SELECT * FROM $bbdb->posts WHERE post_id IN ('" . join( "', '", $_query_post_ids ) . "')"; 
  37. } else { 
  38. // The query is a full SQL query which needs to be executed 
  39. $get_order_from_query = true; 
  40. $_query = $query; 
  41.  
  42. if ( $_query_posts = (array) $bbdb->get_results( $_query ) ) { 
  43. $_appendable_posts = array(); 
  44. foreach ( $_query_posts as $_query_post ) { 
  45. if ( $get_order_from_query ) { 
  46. $ordered_post_ids[] = $_query_post->post_id; 
  47. if ( false === $_post = wp_cache_get( $_query_post->post_id, 'bb_post' ) ) { 
  48. $_appendable_posts[] = $_query_post; 
  49. } else { 
  50. $_cached_posts[$_query_post->post_id] = $_post; 
  51. if ( count( $_appendable_posts ) ) { 
  52. $_query_posts = bb_append_meta( $_appendable_posts, 'post' ); 
  53. foreach( $_query_posts as $_query_post ) { 
  54. wp_cache_add( $_query_post->post_id, $_query_post, 'bb_post' ); 
  55. } else { 
  56. $_query_posts = array(); 
  57. } else { 
  58. $_query_posts = array(); 
  59.  
  60. foreach ( array_merge( $_cached_posts, $_query_posts ) as $_post ) { 
  61. $keyed_posts[$_post->post_id] = $_post; 
  62.  
  63. $the_posts = array(); 
  64. foreach ( $ordered_post_ids as $ordered_post_id ) { 
  65. $the_posts[] = $keyed_posts[$ordered_post_id]; 
  66.  
  67. return $the_posts;