public static function get_top_posts($count = 5, $months_back = False) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; if (is_plugin_active('wp-postviews/wp-postviews.php')) { global $WpBooj_options; if (isset($WpBooj_options['use_WpBoojCache']) && $WpBooj_options['use_WpBoojCache'] == 'on') { $popular_cache = WpBoojCache::check($post_id = 0, $type = 'WpBoojTopPosts'); if ($popular_cache) { return $popular_cache; } } if ($months_back) { $_1_month = 2678400; $time_back = date('Y-m-d', time() - $_1_month * $months_back); $where_append = ' AND `posts`.`post_date` > "' . $time_back . '" '; } else { $where_append = ''; } global $wpdb; $sql = "SELECT\r\n posts.ID,\r\n meta.meta_value,\r\n posts.post_title,\r\n posts.post_name,\r\n posts.post_date\r\n FROM `{$wpdb->prefix}postmeta` as meta\r\n INNER JOIN `{$wpdb->prefix}posts` as posts\r\n ON `meta`.`post_id` = posts.ID\r\n WHERE `meta`.`meta_key` = 'views' " . $where_append . "\r\n ORDER BY CAST( `meta_value` AS DECIMAL ) DESC\r\n LIMIT " . $count; $posts = $wpdb->get_results($sql); $popular = array(); foreach ($posts as $key => $post) { $popular[$key]['post_id'] = $post->ID; $popular[$key]['post_views'] = $post->meta_value; $popular[$key]['post_title'] = $post->post_title; $popular[$key]['post_slug'] = $post->post_name; $popular[$key]['post_date'] = $post->post_date; $popular[$key]['url'] = '/' . WpBoojFindURISegment() . '/' . date('Y/m/', strtotime($popular[$key]['post_date'])) . $popular[$key]['post_slug']; } if (count($popular) == $count && isset($WpBooj_options['use_WpBoojCache']) && $WpBooj_options['use_WpBoojCache'] == 'on') { WpBoojCache::store($post_id = 0, $post_type = 'WpBoojTopPosts', $popular); } return $popular; } else { return 'Please install and enable the plugin "Wp Post Views"'; } }
public static function clear_cache_post_save($post_id) { WpBoojCache::clear_cache($post_id = $post_id); }
public static function get($post_id, $count = 4) { $related_cache = WpBoojCache::check($post_id = $post_id, $type = 'WpBoojRelated'); if ($related_cache) { return $related_cache; } global $wpdb; // Get the cat and tag ids from the given $post_id $tags = wp_get_post_tags($post_id); $tag_ids = ''; foreach ($tags as $key => $tag) { $tag_ids .= $tag->term_id . ','; } $tag_ids = substr($tag_ids, 0, -1); $cats = get_the_category($post_id); $cat_ids = ''; foreach ($cats as $key => $cat) { $cat_ids .= $cat->term_id . ','; } $cat_ids = substr($cat_ids, 0, -1); // NOW WELL LOOK FOR ALL THE OTHER POSTS / PAGES // WHICH USE ANY OF THE SAME CATS / TAGS ORDERD AMMOUNT OF SIMILARITIES // $potential_posts is created here if ($tag_ids == '' && $cat_ids == '') { $full_search = ''; } elseif ($tag_ids == '' && $cat_ids != '') { $full_search = $cat_ids; } elseif ($tag_ids != '' && $cat_ids == '') { $full_search = $tag_ids; } else { $full_search = $tag_ids . "," . $cat_ids; } // Get the term_taxonomy_id from the term_ids $query = "SELECT * FROM `{$wpdb->prefix}term_taxonomy` WHERE `term_id` IN ( " . $full_search . " );"; $rows = $wpdb->get_results($query); $term_taxonomy_ids = ''; foreach ($rows as $key => $row) { $term_taxonomy_ids .= $row->term_taxonomy_id . ','; } $term_taxonomy_ids = substr($term_taxonomy_ids, 0, -1); $query = "SELECT DISTINCT( object_id ), COUNT(*) AS count \r\n\t\t\tFROM `{$wpdb->prefix}term_relationships` \r\n\t\t\tWHERE `term_taxonomy_id` IN( " . $term_taxonomy_ids . " ) \r\n\t\t\t\tAND `object_id` != " . $post_id . "\r\n\t\t\tGROUP BY 1\r\n\t\t\tORDER BY 2 DESC\r\n\t\t\tLIMIT 50"; $rows = $wpdb->get_results($query); $potential_posts = array(); $post_ids = ''; foreach ($rows as $key => $row) { $potential_posts[$row->object_id] = array('post_id' => $row->object_id, 'occurance' => $row->count, 'points' => $row->count * 1000); $post_ids .= $row->object_id . ','; } $post_ids = substr($post_ids, 0, -1); // NOW WE QUERY AGAINST THE POST IDS FROM ABOVE // WE ARE FILTERING OUT ANY POST WHICH IS NOT PUBLISHED // $potential_posts is important here $query2 = "SELECT `ID`, `post_date` FROM `{$wpdb->prefix}posts` \r\n\t\t\tWHERE `ID` IN( " . $post_ids . " ) \r\n\t\t\t\tAND `post_status` = 'publish'\r\n\t\t\t\tAND `post_type` = 'post'\r\n\t\t\tORDER BY `post_date` DESC"; $rows = $wpdb->get_results($query2); foreach ($rows as $key => $row) { if (is_array($potential_posts[$row->ID])) { $date_point_penelty = round((time() - strtotime($row->post_date)) / 86400, 0); $potential_posts[$row->ID]['post'] = $row; $potential_posts[$row->ID]['points'] = $potential_posts[$row->ID]['points'] - $date_point_penelty; } } // HERE WE COMPUTE OUR POINTS $weighted_posts = array(); foreach ($potential_posts as $key => $value) { if (is_object($value['post'])) { $weighted_posts[$key] = $value['points']; } else { unset($potential_posts[$key]); } } array_multisort($weighted_posts, SORT_DESC, $potential_posts); // NOW LETS GROOM THE LIST, MAKE SURE WE HAVE WHAT WE NEED // MAKE SURE WE HAVE ENOUGH CONTENT, OTHER WISE FIND SOME if (count($potential_posts) < $count) { $ids_to_omit = $post_id . ','; $posts_more_needed = $count - count($potential_posts); if (!empty($potential_posts)) { foreach ($potential_posts as $key => $value) { $ids_to_omit .= $value['post']->ID . ','; } } $ids_to_omit = substr($ids_to_omit, 0, -1); //@todo: should just select IDS to simplify $query3 = "SELECT * FROM `{$wpdb->prefix}posts` \r\n\t\t\t\tWHERE `ID` NOT IN( " . $ids_to_omit . " )\r\n\t\t\t\t\tAND `post_status` = 'publish' \r\n\t\t\t\t\tAND `post_type` = 'post' \r\n\t\t\t\tORDER BY `post_date` DESC\r\n\t\t\t\tLIMIT " . $posts_more_needed; $rows = $wpdb->get_results($query3); foreach ($rows as $key => $row) { $potential_posts[$row->ID]['post_id'] = $row->ID; $potential_posts[$row->ID]['post'] = $row; } } $posts = array(); foreach ($potential_posts as $the_post_added) { $posts[] = get_post($the_post_added['post_id']); } if (count($posts) > $count) { $posts = array_slice($posts, 0, $count, True); } // store a cached version if we found content if (count($posts) == $count) { WpBoojCache::store($post_id = $post_id, $post_type = 'WpBoojRelated', $posts); } return $posts; }