web/wp-content/plugins/related-posts-by-category/related_posts.php
author hurons@caf4f556-3d62-0410-8435-a86758001935
Tue, 02 Feb 2010 15:44:16 +0000
branchwordpress
changeset 132 4d4862461b8d
parent 123 561aa6d282f6
permissions -rw-r--r--
Update Wordpress 2.9.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
109
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     1
<?php
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     2
/*
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     3
Plugin Name: Related Posts by Category
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     4
Plugin URI: http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     5
Description: The WordPress Plugin for similar posts grouped by category. It's small. It's fast. Really.
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     6
Author: Sergej M&uuml;ller
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     7
Version: 0.4
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     8
Author URI: http://www.wpseo.org
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
     9
*/
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    10
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    11
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    12
function related_posts_by_category($params, $post_id = 0) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    13
$entries = array();
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    14
$output = '';
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    15
$limit = 0;
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    16
$type = '';
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    17
$order = '';
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    18
$orderby = '';
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    19
$post_id = intval($post_id);
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    20
if (!$post_id) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    21
$post_id = $GLOBALS['post']->ID;
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    22
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    23
if (isset($params['limit'])) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    24
$limit = intval($params['limit']);
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    25
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    26
if (isset($params['type']) && !empty($params['type'])) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    27
$type = ($params['type'] == 'page' ? 'page' : 'post');
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    28
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    29
if (isset($params['order']) && !empty($params['order'])) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    30
$order = (strtoupper($params['order']) == 'DESC' ? 'DESC' : 'ASC');
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    31
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    32
if (isset($params['orderby']) && !empty($params['orderby']) && preg_match('/^[a-zA-Z_]+$/', $params['orderby'])) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    33
$orderby = $params['orderby'];
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    34
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    35
$entries = $GLOBALS['wpdb']->get_results(
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    36
sprintf(
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    37
"SELECT DISTINCT object_id, post_title FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t, {$GLOBALS['wpdb']->posts} p WHERE t.term_id IN (SELECT t.term_id FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'category' AND r.object_id = $post_id) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'publish' AND p.ID = r.object_id AND object_id <> $post_id %s %s %s",
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    38
($type ? ("AND p.post_type = '" .$type. "'") : ''),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    39
($orderby ? ('ORDER BY ' .(strtoupper($params['orderby']) == 'RAND' ? 'RAND()' : $orderby. ' ' .$order)) : ''),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    40
($limit ? ('LIMIT ' .$limit) : '')
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    41
),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    42
OBJECT
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    43
);
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    44
if ($entries) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    45
foreach ($entries as $entry) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    46
$output .= sprintf(
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    47
'%s<a href="%s" %s title="%s">%s%s%s</a>%s',
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    48
isset($params['before']) ? $params['before'] : '',
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    49
get_permalink($entry->object_id),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    50
(isset($params['rel']) ? ('rel="' .$params['rel']. '"') : ''),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    51
str_replace('"', '&quot;', $entry->post_title),
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    52
isset($params['inside']) ? $params['inside'] : '',
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    53
$entry->post_title,
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    54
isset($params['outside']) ? $params['outside'] : '',
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    55
isset($params['after']) ? $params['after'] : ''
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    56
);
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    57
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    58
} else {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    59
$output = $params['message'];
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    60
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    61
if (isset($params['echo']) === true && $params['echo']) {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    62
echo $output;
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    63
} else {
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    64
return $output;
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    65
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    66
}
03b0d1493584 wordpress 2.8 ()
hurons@caf4f556-3d62-0410-8435-a86758001935
parents:
diff changeset
    67
?>