/***********************************************************************************/
/* get_posts hack
/***********************************************************************************/
function pt_get_posts($args) {
global $wpdb;
$defaults = array(
'showposts' => 5, 'offset' => 0,
'cat' => '', 'orderby' => 'post_date',
'order' => 'DESC', 'include' => '',
'exclude' => '', 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'post_status' => 'publish', 'post_parent' => 0
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$showposts = (int) $showposts;
$ecat = explode(',', $cat);
$ducky = (int) $ecat[0];
if ($ducky < 0) $exclcat = 'NOT '; else $exclcat = '';
$cat = implode(',',$ecat);
$offset = (int) $offset;
$post_parent = (int) $post_parent;
$inclusions = '';
if ( !empty($include) ) {
$offset = 0; //ignore offset, category, exclude, meta_key, and meta_value, post_parent if using include
$cat = 0;
$exclude = '';
$meta_key = '';
$meta_value = '';
$post_parent = 0;
$incposts = preg_split('/[\s,]+/',$include);
$showposts = count($incposts); // only the number of posts included
if ( count($incposts) ) {
foreach ( $incposts as $incpost ) {
if (empty($inclusions))
$inclusions = ' AND ( ID = ' . intval($incpost) . ' ';
else
$inclusions .= ' OR ID = ' . intval($incpost) . ' ';
}
}
}
if (!empty($inclusions))
$inclusions .= ')';
$exclusions = '';
if ( !empty($exclude) ) {
$exposts = preg_split('/[\s,]+/',$exclude);
if ( count($exposts) ) {
foreach ( $exposts as $expost ) {
if (empty($exclusions))
$exclusions = ' AND ( ID <> ' . intval($expost) . ' ';
else
$exclusions .= ' AND ID <> ' . intval($expost) . ' ';
}
}
}
if (!empty($exclusions))
$exclusions .= ')';
$query = "SELECT DISTINCT * FROM $wpdb->posts ";
$query .= empty( $cat ) ? '' : ", $wpdb->term_relationships, $wpdb->term_taxonomy ";
$query .= empty( $meta_key ) ? '' : ", $wpdb->postmeta ";
$query .= " WHERE 1=1 ";
$query .= empty( $post_type ) ? '' : "AND post_type = '$post_type' ";
$query .= empty( $post_status ) ? '' : "AND post_status = '$post_status' ";
$query .= "$exclusions $inclusions " ;
$query .= empty( $cat ) ? '' : "AND ($wpdb->posts.ID = $wpdb->term_relationships.object_id AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id AND $wpdb->term_taxonomy.term_id ".$exclcat."IN (" . $cat. ") AND $wpdb->term_taxonomy.taxonomy = 'category') ";
$query .= empty( $post_parent ) ? '' : "AND $wpdb->posts.post_parent = '$post_parent' ";
$query .= !empty( $meta_key ) && !empty($meta_value) ? " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '$meta_key' AND $wpdb->postmeta.meta_value = '$meta_value' )" : '';
$query .= !empty( $meta_key ) && empty($meta_value) ? " AND ($wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key LIKE '$meta_key%' )" : '';
$query .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . ' ' . $order;
if ( 0 < $showposts )
$query .= " LIMIT " . $offset . ',' . $showposts;
$posts = $wpdb->get_results($query);
update_post_caches($posts);
return $posts;
}