/** * Add related posts HTML to the content * * @param string $content * @since 2.1 * @return string */ function ep_related_posts_filter_content($content) { if (is_search() || is_home() || is_archive() || is_category()) { return $content; } $post_id = get_the_ID(); $cache_key = md5('related_posts_' . $post_id); $related_posts = wp_cache_get($cache_key, 'ep-related-posts'); if (false === $related_posts) { $related_posts = ep_find_related($post_id); wp_cache_set($cache_key, $related_posts, 'ep-related-posts', 300); } $html = ep_related_get_html($related_posts); return $content . "\n" . $html; }
/** * Test for related post args filter */ public function testFindRelatedPostFilter() { $post_id = ep_create_and_sync_post(array('post_content' => 'findme test 1')); ep_create_and_sync_post(array('post_content' => 'findme test 2')); ep_create_and_sync_post(array('post_content' => 'findme test 3', 'post_type' => 'page')); ep_refresh_index(); ep_activate_module('related_posts'); EP_Modules::factory()->setup_modules(); add_filter('ep_find_related_args', array($this, 'find_related_posts_filter'), 10, 1); $related = ep_find_related($post_id); $this->assertEquals(2, sizeof($related)); remove_filter('ep_find_related_args', array($this, 'find_related_posts_filter')); $related = ep_find_related($post_id); $this->assertEquals(3, sizeof($related)); }