Skip to content

Instantly share code, notes, and snippets.

@ideag ideag/tinyrelated.php Secret
Last active Aug 29, 2015

What would you like to do?
Plugin Name: tinyRelated
Plugin URI:
Description: A tiny and simple related posts plugin.
Author: Arūnas Liuiza
Version: 0.2.0
Author URI:
License: GPL2
Copyright 2014 Arūnas Liuiza (email :
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
// block direct access to plugin file
defined('ABSPATH') or die("No script kiddies please!");
// add filter to automatically show related posts after post content
add_filter( 'the_content', array( 'tinyRelated', 'append_list') );
// main plugin class
class tinyRelated {
// options
public static $options = array(
'general_count' => 5,
'general_show_after' => true,
// add list to the end of post_content
public static function append_list( $content ) {
if (is_singular('post')) {
if ( self::$options['general_show_after'] ) {
$post_id = get_the_id();
$related = self::build_list($post_id);
$content .= "\r\n".$related;
return $content;
// build html for related post list
public static function build_list($post) {
if (is_numeric($post)) {
$post = get_post($post);
$posts = tinyRelated::get_list($post);
$return .= apply_filters( 'tinyrelated_list_title', '<h3>'.'Related posts'.'</h3>' );
$return .= apply_filters( 'tinyrelated_list_start', '<ul class="tinyrelated">' );
foreach ($posts as $related_post) {
if (is_numeric($related_post)) {
$related_post = get_post($related_post);
if (!$related_post || $related_post->post_status != 'publish') {
$return .= sprintf(
apply_filters( 'tinyrelated_list_item', '<li><a href="%1$s">%2$s</a></li>' ),
$return .= apply_filters( 'tinyrelated_list_end', '</ul>' );
return $return;
// get array of related posts - only dummy for now
public static function get_list($post) {
if (is_numeric($post)) {
$post = get_post($post);
$args = array(
'post_type' => $post->post_type,
'post_status' => 'publish',
'posts_per_page' => self::$options['general_count'],
'orderby' => 'rand',
$return = new WP_Query($args);
return $return->posts;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.