Make WordPress Core

Ticket #52: 0000052-get_posts.php

File 0000052-get_posts.php, 1.7 KB (added by iioshius, 21 years ago)
Line 
1<?php
2function get_posts($args) {
3 global $wpdb, $tableposts, $tablepost2cat;
4 parse_str($args, $r);
5
6 $now = current_time('mysql');
7
8 //define the base queries and conditionals
9
10 $strQuery = "SELECT DISTINCT * FROM $tableposts";
11
12 $strCategoryQuerySelect = ", $tablepost2cat";
13
14 $strBaseQueryConditional = " WHERE post_date <= '$now' AND (post_status = 'publish')";
15 $strCategoryQueryConditional = " AND ( ( `$tableposts`.`ID` = `$tablepost2cat`.`post_id` ) AND ( `$tablepost2cat`.`category_id` = '" . $r['category'] . "' ) ) ";
16 $strExcludeCategoryQueryConditional = " AND ( ( `$tableposts`.`ID` = `$tablepost2cat`.`post_id` ) AND ( `$tablepost2cat`.`category_id` <> '" . $r['excludecategory'] . "' ) ) ";
17
18 //update the base query with category & exclude category optional queries
19
20 if ( ( isset( $r['category'] ) ) || ( isset( $r['excludecategory'] ) ) ) $strQuery = $strQuery . $strCategoryQuerySelect;
21
22 $strQuery = $strQuery . $strBaseQueryConditional;
23
24 if ( isset( $r['category'] ) ) { $strQuery = $strQuery . $strCategoryQueryConditional; }
25 if ( isset( $r['excludecategory'] ) ) { $strQuery = $strQuery . $strExcludeCategoryQueryConditional; }
26
27 //update the base query with offset, numbering, and ordering options
28
29 if (!isset($r['numberposts'])) $r['numberposts'] = 5;
30 if (!isset($r['offset'])) $r['offset'] = 0;
31 // The following not implemented yet
32 if (!isset($r['orderby'])) $r['orderby'] = '';
33 if (!isset($r['order'])) $r['order'] = '';
34
35 $strBaseQuerySuffix = "GROUP BY $tableposts.ID ORDER BY post_date DESC LIMIT " . $r['offset'] . ',' . $r['numberposts'];
36
37 $strQuery = $strQuery . $strBaseQuerySuffix;
38
39 $posts = $wpdb->get_results( $strQuery );
40
41 return $posts;
42}
43?>