Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active January 8, 2016 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/f11e522b8e0e1068b037 to your computer and use it in GitHub Desktop.
Save igorbenic/f11e522b8e0e1068b037 to your computer and use it in GitHub Desktop.
How to create WordPress Related Posts | Article on www.ibenic.com
<?php
add_filter( 'the_content','ibenic_related_content' );
function ibenic_related_content( $content ){
// Check if the content is of a single article
if( is_singular( 'post' ) ) {
global $post;
// Variable that will be used to create the HTML of related content
$related_content = "";
$articles = ibenic_get_related_content( $post->ID );
if( ! empty( $articles ) ){
$related_content .= "<h2>" . __( "Related Content","your_textdomain" ) . "</h2>";
$related_content .= "<ul>";
foreach($articles as $article){
$related_content .= "<li><a href='" . get_permalink( $article->ID ) . "'>";
$related_content .= "<h3>" . $article->post_title . "</h3>";
// You can set here other $article information such as a thumbnail, category, date etc.
$related_content .= "</a></li>";
}
$related_content .= "</ul>";
$content .= $related_content;
return $content;
}
} else {
return $content;
}
}
<?php
// ....
$sql = "SELECT * FROM $wpdb->posts WHERE ID IN (
SELECT post_id FROM (
SELECT t1.object_id as post_id, count(t1.term_taxonomy_id) as term_match
FROM $wpdb->term_relationships as t1
INNER JOIN $wpdb->term_relationships as t2 on t1.term_taxonomy_id = t2.term_taxonomy_id
-- Joining the table that describes the taxonomies
INNER JOIN $wpdb->term_taxonomy as x1 on x1.term_taxonomy_id = t2.term_taxonomy_id
-- Filtering the query with another WHERE condition where we want only terms of taxonomy with the name 'tag'
WHERE t2.object_id = $ID AND x1.taxonomy = 'tag'
GROUP BY t1.object_id
HAVING term_match > 4 AND t1.object_id != $ID
LIMIT 20
) as related_content
)";
//...
<?php
$sql = "SELECT * FROM $wpdb->posts WHERE ID IN (
SELECT post_id FROM (
SELECT t1.object_id as post_id, count(t1.term_taxonomy_id) as term_match
FROM $wpdb->term_relationships as t1
INNER JOIN $wpdb->term_relationships as t2 on t1.term_taxonomy_id = t2.term_taxonomy_id
-- Joining the table that describes the taxonomies
INNER JOIN $wpdb->term_taxonomy as x1 on x1.term_taxonomy_id = t2.term_taxonomy_id
-- Joining the table that that contains meta data to each article by the ID of the article
-- and the post_id of the meta data table
INNER JOIN $wpdb->postmeta as m1 on m1.post_id = t1.object_id
-- Filtering the query with another WHERE condition where we want only terms of taxonomy with the name 'tag'
WHERE t2.object_id = $ID AND x1.taxonomy = 'tag'
-- Filtering the query with yet other WHERE conditions where we want to limit the query to a specific meta data
AND m1.meta_key = 'META_KEY_WE_WANT_TO_LOOK_FOR'
AND m1.meta_value = 'META_VALUE_of_the_META_KEY_WE_WANT_TO_LOOK_FOR'
GROUP BY t1.object_id
HAVING term_match > 4 AND t1.object_id != $ID
LIMIT 20
) as related_content
)";
<?php
//Main Query
SELECT * FROM wp_posts WHERE ID IN(
//Subqery
SELECT post_id FROM (
/*Get IDs of articles and the number of same terms with the main article*/
SELECT t1.object_id as post_id, count(t1.term_taxonomy_id) as term_match
FROM wp_term_relationships as t1
/*Join the table that represents the list of all articles with the table of the main article*/
INNER JOIN wp_term_relationships as t2 on t1.term_taxonomy_id = t2.term_taxonomy_id
/*Filter the query by limit the t2 table only to the main article*/
WHERE t2.object_id = MAIN_ARTICLE_ID
/*Group by IDs of the table t1 (all articles) so that we do not get rows with duplicate IDs*/
GROUP BY t1.object_id
/* Give a condition to the query so that the query returs only articles that have at least 5 of terms that are common
to the main article. We are also excluding the ID of our main article from the list of all articles
*/
HAVING term_match > 4 AND t1.object_id != MAIN_ARTICLE_ID
/*Limit the result to 20 rows */
LIMIT 20
) as related_content
)
<?php
/*Get IDs of articles and the number of same terms with the main article*/
SELECT t1.object_id as post_id, count(t1.term_taxonomy_id) as term_match
FROM wp_term_relationships as t1
/*Join the table that represents the list of all articles with the table of the main article*/
INNER JOIN wp_term_relationships as t2 on t1.term_taxonomy_id = t2.term_taxonomy_id
/*Filter the query by limit the t2 table only to the main article*/
WHERE t2.object_id = MAIN_ARTICLE_ID
/*Group by IDs of the table t1 (all articles) so that we do not get rows with duplicate IDs*/
GROUP BY t1.object_id
/* Give a condition to the query so that the query returs only articles that have at least 5 of terms that are common
to the main article. We are also excluding the ID of our main article from the list of all articles
*/
HAVING term_match > 4 AND t1.object_id != MAIN_ARTICLE_ID
/*Limit the result to 20 rows */
LIMIT 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment