Add meta_key and meta_value options to get_posts() and get_pages(). Props MichaelH. fixes #2563
git-svn-id: http://svn.automattic.com/wordpress/trunk@3656 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
4ce7df114d
commit
3c61e4b10d
|
@ -1451,18 +1451,19 @@ function get_posts($args) {
|
|||
$r = &$args;
|
||||
else
|
||||
parse_str($args, $r);
|
||||
parse_str($args, $r);
|
||||
|
||||
$defaults = array('numberposts' => 5, 'offset' => 0, 'category' => '',
|
||||
'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '');
|
||||
'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' =>'');
|
||||
$r = array_merge($defaults, $r);
|
||||
extract($r);
|
||||
|
||||
$inclusions = '';
|
||||
if ( !empty($include) ) {
|
||||
$offset = 0; //ignore offset, category, and exclude params if using include
|
||||
$offset = 0; //ignore offset, category, exclude, meta_key, and meta_value params if using include
|
||||
$category = '';
|
||||
$exclude = '';
|
||||
$meta_key = '';
|
||||
$meta_value = '';
|
||||
$incposts = preg_split('/[\s,]+/',$include);
|
||||
$numberposts = count($incposts); // only the number of posts included
|
||||
if ( count($incposts) ) {
|
||||
|
@ -1492,12 +1493,15 @@ function get_posts($args) {
|
|||
if (!empty($exclusions))
|
||||
$exclusions .= ')';
|
||||
|
||||
$posts = $wpdb->get_results(
|
||||
"SELECT DISTINCT * FROM $wpdb->posts " .
|
||||
( empty( $category ) ? "" : ", $wpdb->post2cat " ) .
|
||||
" WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " .
|
||||
( empty( $category ) ? "" : "AND $wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. " " ) .
|
||||
" GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts );
|
||||
$query ="SELECT DISTINCT * FROM $wpdb->posts " ;
|
||||
$query .= ( empty( $category ) ? "" : ", $wpdb->post2cat " ) ;
|
||||
$query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
|
||||
$query .= " WHERE (post_type = 'post' AND post_status = 'publish') $exclusions $inclusions " ;
|
||||
$query .= ( empty( $category ) ? "" : "AND ($wpdb->posts.ID = $wpdb->post2cat.post_id AND $wpdb->post2cat.category_id = " . $category. ") " ) ;
|
||||
$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 .= " GROUP BY $wpdb->posts.ID ORDER BY " . $orderby . " " . $order . " LIMIT " . $offset . ',' . $numberposts ;
|
||||
|
||||
$posts = $wpdb->get_results($query);
|
||||
|
||||
update_post_caches($posts);
|
||||
|
||||
|
|
|
@ -306,14 +306,16 @@ function &get_pages($args = '') {
|
|||
parse_str($args, $r);
|
||||
|
||||
$defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title',
|
||||
'hierarchical' => 1, $exclude => '', $include => '');
|
||||
'hierarchical' => 1, 'exclude' => '', 'include' => '', 'meta_key' => '', 'meta_value' => '');
|
||||
$r = array_merge($defaults, $r);
|
||||
extract($r);
|
||||
|
||||
$inclusions = '';
|
||||
if ( !empty($include) ) {
|
||||
$child_of = 0; //ignore child_of and exclude params if using include
|
||||
$child_of = 0; //ignore child_of, exclude, meta_key, and meta_value params if using include
|
||||
$exclude = '';
|
||||
$meta_key = '';
|
||||
$meta_value = '';
|
||||
$incpages = preg_split('/[\s,]+/',$include);
|
||||
if ( count($incpages) ) {
|
||||
foreach ( $incpages as $incpage ) {
|
||||
|
@ -342,11 +344,13 @@ function &get_pages($args = '') {
|
|||
if (!empty($exclusions))
|
||||
$exclusions .= ')';
|
||||
|
||||
$pages = $wpdb->get_results("SELECT * " .
|
||||
"FROM $wpdb->posts " .
|
||||
"WHERE post_type = 'page' AND post_status = 'publish' " .
|
||||
"$exclusions $inclusions" .
|
||||
"ORDER BY " . $sort_column . " " . $sort_order);
|
||||
$query = "SELECT * FROM $wpdb->posts " ;
|
||||
$query .= ( empty( $meta_key ) ? "" : ", $wpdb->postmeta " ) ;
|
||||
$query .= " WHERE (post_type = 'page' AND post_status = 'publish') $exclusions $inclusions " ;
|
||||
$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 .= " ORDER BY " . $sort_column . " " . $sort_order ;
|
||||
|
||||
$pages = $wpdb->get_results($query);
|
||||
|
||||
if ( empty($pages) )
|
||||
return array();
|
||||
|
|
Loading…
Reference in New Issue