Created
January 11, 2015 21:22
-
-
Save folletto/714bd49a49a7666dd495 to your computer and use it in GitHub Desktop.
WordPress: Create a Custom Loop for Related Posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Notes: | |
* 1. This isn't an universal function: you might have to add your own db field. | |
* I tried to keep just a few to optimize. | |
* 2. This is a more advanced function from the ones you usually find around, because it | |
* count the number of matching tags to sort the related articles, doesn't just query | |
* and sort for the most recent one with any of the tags. | |
*/ | |
function loop_related_posts( $posts_per_page = 3, $category = null, $post_ID = 0 ) { | |
/**************************************************************************************************** | |
* Get the post related to the current-looping post's matching tags. | |
* Creates its own loop. | |
* | |
* USAGE: | |
* <?php while ( loop_related_posts( 3 ) ) : ?> | |
* your standard template code | |
* <?php endwhile; ?> | |
*/ | |
global $wpdb; | |
global $post; | |
static $current_post = false; | |
static $results = array(); | |
// ****** Terminate command. | |
// Done on new loop to allow everything setup correctly in the last loop. | |
if ( !$current_post === false && $current_post >= count( $results ) ) { | |
$current_post = false; | |
wp_reset_postdata(); | |
return $current_post; | |
} | |
if ( $current_post === false ) { | |
// ****** Get current post data | |
$post_ID = intval( $post_ID ); | |
if ( $post_ID == 0 ) { | |
$post_ID = (int)$post->ID; | |
} | |
// ****** Cache | |
$cachekey = 'relatedposts-' . $post_id . '-' . md5( $posts_per_page . "other" ); | |
$results = wp_cache_get( $cachekey, "theme-relatedposts" ); | |
// ****** No cache? Query! | |
if ( $results === false ) { | |
// ****** Set variables | |
if ( $category ) $category = "AND tm.name = '{$category}'"; | |
$tags = get_the_tags( $post_ID ); | |
if ( $tags == false || $post_ID == 0 ) return false; // --> EXIT | |
// ****** SQL Tags list | |
$taglist = array(); | |
foreach ( $tags as $tag ) { $taglist[] = '"' . $tag->term_id . '"'; }; | |
$taglist = join( $taglist, ', ' ); | |
// ****** Toggle Extended Content Query | |
$is_extended_content = ''; | |
if ( true ) $is_extended_content = ', p.post_content'; | |
$results = $wpdb->get_results( | |
"SELECT DISTINCT COUNT(tr.object_id) AS rank, p.ID, p.post_title, p.post_excerpt, p.comment_count, p.post_name, p.post_date {$is_extended_content} | |
FROM {$wpdb->posts} AS p | |
INNER JOIN {$wpdb->term_relationships} AS tr ON p.ID = tr.object_id | |
INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id | |
INNER JOIN {$wpdb->terms} AS tm ON tm.term_id = tt.term_id | |
WHERE | |
tt.taxonomy = 'post_tag' | |
AND (tt.term_id IN ({$taglist})) | |
AND p.post_status = 'publish' | |
AND p.post_type = 'post' | |
AND p.ID != {$post_ID} | |
AND p.post_date < '" . current_time('mysql') . "' | |
GROUP BY tr.object_id | |
ORDER BY rank DESC | |
LIMIT 0, {$posts_per_page} | |
"); | |
// ****** Cache | |
wp_cache_set($cachekey, $results, "theme-relatedposts"); | |
} | |
if ( !$results ) return false; // --> EXIT | |
// ****** We have data, first loop is on! | |
$current_post = 0; | |
} | |
if ( $current_post !== false ) { // This isn't 'else' because can be initialized just above. | |
// ****** Loop | |
$current_post++; | |
$post = $results[$current_post - 1]; | |
setup_postdata( $post ); | |
} | |
return $current_post; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment