root/afridex/plugins/get-a-post.php @ 23

Revision 21, 2.7 kB (checked in by admin, 18 years ago)
Line 
1<?php
2/*
3Plugin Name: Get-a-Post
4Plugin URI: http://guff.szub.net/get-a-post
5Description: Display a specific post (or Page) with standard WP template tags.
6Version: R1.4
7Author: Kaf Oseo
8Author URI: http://szub.net
9
10        Copyright (c) 2004-2006, 2008 Kaf Oseo (http://szub.net)
11        Get-a-Post is released under the GNU General Public License, version 2 (GPL2)
12        http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
13
14        This is a WordPress plugin (http://wordpress.org).
15
16~Changelog:
17R1.4 (Jan-27-2008)
18'Rich' release. Adds a 'GETRANDOM' id, which retrieves (surprise) a
19random published post. Random rules!
20
21R1.3 (Apr-21-2006)
22Bug fix (sorry for the delay). Added a 'GETSTICKY' id, which gets a
23post using 'sticky' for a custom field key and '1' for the value.
24
25R1.2 (Mar-03-2006)
26Use 'GETPAGE' (all caps) as argument for latest Page, and 'GETPOST'
27or no argument for latest post. Tentative suppport for WordPress 2.1.
28
29R1.1 (May-04-2005]
30Caches post-meta (custom field) data for use with other plugins or
31template tags. Code tweaks. Explicit GPL licensing.
32
33R1 (Mar-01-2005)
34"Clementine" release. Handles Pages under WordPress 1.5+. Accepts a
35post name (Post/Page slug) or numeric ID as argument.
36
370.3 (Jan-29-2005)
38Intializes post object data to avoid needing to run get_a_post() in
39"The Loop" of a template.
40
410.2 (Jan-28-2005)
42Changes for support under WordPress 1.5.
43*/
44
45function get_a_post($id='GETPOST') {
46        global $post, $tableposts, $tablepostmeta, $wp_version, $wpdb;
47
48        if($wp_version < 1.5)
49                $table = $tableposts;
50        else
51                $table = $wpdb->posts;
52
53        $now = current_time('mysql');
54        $name_or_id = '';
55        $orderby = 'post_date';
56
57        if( !$id || 'GETPOST' == $id || 'GETRANDOM' == $id ) {
58                if( $wp_version < 2.1 )
59                        $query_suffix = "post_status = 'publish'";
60                else
61                        $query_suffix = "post_type = 'post' AND post_status = 'publish'";
62        } elseif('GETPAGE' == $id) {
63                if($wp_version < 2.1)
64                        $query_suffix = "post_status = 'static'";
65                else
66                        $query_suffix = "post_type = 'page' AND post_status = 'publish'";
67        } elseif('GETSTICKY' == $id) {
68                if($wp_version < 1.5)
69                        $table .= ', ' . $tablepostmeta;
70                else
71                        $table .= ', ' . $wpdb->postmeta;
72                $query_suffix = "ID = post_id AND meta_key = 'sticky' AND meta_value = 1";
73        } else {
74                $query_suffix = "(post_status = 'publish' OR post_status = 'static')";
75
76                if(is_numeric($id)) {
77                        $name_or_id = "ID = '$id' AND";
78                } else {
79                        $name_or_id = "post_name = '$id' AND";
80                }
81        }
82
83        if('GETRANDOM' == $id)
84                $orderby = 'RAND()';
85
86        $post = $wpdb->get_row("SELECT * FROM $table WHERE $name_or_id post_date <= '$now' AND $query_suffix ORDER BY $orderby DESC LIMIT 1");
87        get_post_custom($post->ID);
88
89        if($wp_version < 1.5)
90                start_wp();
91        else
92                setup_postdata($post);
93}
94?>
Note: See TracBrowser for help on using the browser.