From ff7c3f55e4083aaf1e5e12c8919348e50cc84687 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 21 Mar 2006 22:46:38 +0000 Subject: [PATCH] Add meta_key and meta_value options to get_posts() and get_pages(). Props MichaelH. fixes #2563 git-svn-id: https://develop.svn.wordpress.org/trunk@3656 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/functions.php | 22 +++++++++++++--------- wp-includes/template-functions-post.php | 18 +++++++++++------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 322ba46749..ec8a192e1b 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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); diff --git a/wp-includes/template-functions-post.php b/wp-includes/template-functions-post.php index 794eded6ac..11a9232519 100644 --- a/wp-includes/template-functions-post.php +++ b/wp-includes/template-functions-post.php @@ -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();