コード例 #1
0
ファイル: author-template.php プロジェクト: gcorral/hivequeen
/**
 * List all the authors of the blog, with several options available.
 *
 * @link https://codex.wordpress.org/Template_Tags/hq_list_authors
 *
 * @since 0.0.1
 *
 * @global hqdb $hqdb
 *
 * @param string|array $args {
 *     Optional. Array or string of default arguments.
 *
 *     @type string $orderby       How to sort the authors. Accepts 'nicename', 'email', 'url', 'registered',
 *                                 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name',
 *                                 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'.
 *     @type string $order         Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'.
 *     @type int    $number        Maximum authors to return or display. Default empty (all authors).
 *     @type bool   $optioncount   Show the count in parenthesis next to the author's name. Default false.
 *     @type bool   $exclude_admin Whether to exclude the 'admin' account, if it exists. Default false.
 *     @type bool   $show_fullname Whether to show the author's full name. Default false.
 *     @type bool   $hide_empty    Whether to hide any authors with no posts. Default true.
 *     @type string $feed          If not empty, show a link to the author's feed and use this text as the alt
 *                                 parameter of the link. Default empty.
 *     @type string $feed_image    If not empty, show a link to the author's feed and use this image URL as
 *                                 clickable anchor. Default empty.
 *     @type string $feed_type     The feed type to link to, such as 'rss2'. Defaults to default feed type.
 *     @type bool   $echo          Whether to output the result or instead return it. Default true.
 *     @type string $style         If 'list', each author is wrapped in an `<li>` element, otherwise the authors
 *                                 will be separated by commas.
 *     @type bool   $html          Whether to list the items in HTML form or plaintext. Default true.
 *     @type string $exclude       An array, comma-, or space-separated list of author IDs to exclude. Default empty.
 *     @type string $exclude       An array, comma-, or space-separated list of author IDs to include. Default empty.
 * }
 * @return string|void The output, if echo is set to false.
 */
function hq_list_authors($args = '')
{
    global $hqdb;
    $defaults = array('orderby' => 'name', 'order' => 'ASC', 'number' => '', 'optioncount' => false, 'exclude_admin' => true, 'show_fullname' => false, 'hide_empty' => true, 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, 'style' => 'list', 'html' => true, 'exclude' => '', 'include' => '');
    $args = hq_parse_args($args, $defaults);
    $return = '';
    $query_args = hq_array_slice_assoc($args, array('orderby', 'order', 'number', 'exclude', 'include'));
    $query_args['fields'] = 'ids';
    $authors = get_users($query_args);
    $author_count = array();
    foreach ((array) $hqdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM {$hqdb->posts} WHERE " . get_private_posts_cap_sql('post') . " GROUP BY post_author") as $row) {
        $author_count[$row->post_author] = $row->count;
    }
    foreach ($authors as $author_id) {
        $author = get_userdata($author_id);
        if ($args['exclude_admin'] && 'admin' == $author->display_name) {
            continue;
        }
        $posts = isset($author_count[$author->ID]) ? $author_count[$author->ID] : 0;
        if (!$posts && $args['hide_empty']) {
            continue;
        }
        if ($args['show_fullname'] && $author->first_name && $author->last_name) {
            $name = "{$author->first_name} {$author->last_name}";
        } else {
            $name = $author->display_name;
        }
        if (!$args['html']) {
            $return .= $name . ', ';
            continue;
            // No need to go further to process HTML.
        }
        if ('list' == $args['style']) {
            $return .= '<li>';
        }
        $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr(sprintf(__("Posts by %s"), $author->display_name)) . '">' . $name . '</a>';
        if (!empty($args['feed_image']) || !empty($args['feed'])) {
            $link .= ' ';
            if (empty($args['feed_image'])) {
                $link .= '(';
            }
            $link .= '<a href="' . get_author_feed_link($author->ID, $args['feed_type']) . '"';
            $alt = '';
            if (!empty($args['feed'])) {
                $alt = ' alt="' . esc_attr($args['feed']) . '"';
                $name = $args['feed'];
            }
            $link .= '>';
            if (!empty($args['feed_image'])) {
                $link .= '<img src="' . esc_url($args['feed_image']) . '" style="border: none;"' . $alt . ' />';
            } else {
                $link .= $name;
            }
            $link .= '</a>';
            if (empty($args['feed_image'])) {
                $link .= ')';
            }
        }
        if ($args['optioncount']) {
            $link .= ' (' . $posts . ')';
        }
        $return .= $link;
        $return .= 'list' == $args['style'] ? '</li>' : ', ';
    }
    $return = rtrim($return, ', ');
    if (!$args['echo']) {
        return $return;
    }
    echo $return;
}
コード例 #2
0
ファイル: comment.php プロジェクト: gcorral/hivequeen
 /**
  * Get a list of comments matching the query vars.
  *
  * @since 0.0.1
  * @access public
  *
  * @global hqdb $hqdb HiveQueen database abstraction object.
  *
  * @return int|array The list of comments.
  */
 public function get_comments()
 {
     global $hqdb;
     $groupby = '';
     $this->parse_query();
     // Parse meta query
     $this->meta_query = new HQ_Meta_Query();
     $this->meta_query->parse_query_vars($this->query_vars);
     /**
      * Fires before comments are retrieved.
      *
      * @since 0.0.1
      *
      * @param HQ_Comment_Query &$this Current instance of HQ_Comment_Query, passed by reference.
      */
     do_action_ref_array('pre_get_comments', array(&$this));
     // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
     $this->meta_query->parse_query_vars($this->query_vars);
     if (!empty($this->meta_query->queries)) {
         $meta_query_clauses = $this->meta_query->get_sql('comment', $hqdb->comments, 'comment_ID', $this);
     }
     // $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
     $key = md5(serialize(hq_array_slice_assoc($this->query_vars, array_keys($this->query_var_defaults))));
     $last_changed = hq_cache_get('last_changed', 'comment');
     if (!$last_changed) {
         $last_changed = microtime();
         hq_cache_set('last_changed', $last_changed, 'comment');
     }
     $cache_key = "get_comments:{$key}:{$last_changed}";
     if ($cache = hq_cache_get($cache_key, 'comment')) {
         $this->comments = $cache;
         return $this->comments;
     }
     $where = array();
     // Assemble clauses related to 'comment_approved'.
     $approved_clauses = array();
     // 'status' accepts an array or a comma-separated string.
     $status_clauses = array();
     $statuses = $this->query_vars['status'];
     if (!is_array($statuses)) {
         $statuses = preg_split('/[\\s,]+/', $statuses);
     }
     // 'any' overrides other statuses.
     if (!in_array('any', $statuses)) {
         foreach ($statuses as $status) {
             switch ($status) {
                 case 'hold':
                     $status_clauses[] = "comment_approved = '0'";
                     break;
                 case 'approve':
                     $status_clauses[] = "comment_approved = '1'";
                     break;
                 case 'all':
                 case '':
                     $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )";
                     break;
                 default:
                     $status_clauses[] = $hqdb->prepare("comment_approved = %s", $status);
                     break;
             }
         }
         if (!empty($status_clauses)) {
             $approved_clauses[] = '( ' . implode(' OR ', $status_clauses) . ' )';
         }
     }
     // User IDs or emails whose unapproved comments are included, regardless of $status.
     if (!empty($this->query_vars['include_unapproved'])) {
         $include_unapproved = $this->query_vars['include_unapproved'];
         // Accepts arrays or comma-separated strings.
         if (!is_array($include_unapproved)) {
             $include_unapproved = preg_split('/[\\s,]+/', $include_unapproved);
         }
         $unapproved_ids = $unapproved_emails = array();
         foreach ($include_unapproved as $unapproved_identifier) {
             // Numeric values are assumed to be user ids.
             if (is_numeric($unapproved_identifier)) {
                 $approved_clauses[] = $hqdb->prepare("( user_id = %d AND comment_approved = '0' )", $unapproved_identifier);
                 // Otherwise we match against email addresses.
             } else {
                 $approved_clauses[] = $hqdb->prepare("( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier);
             }
         }
     }
     // Collapse comment_approved clauses into a single OR-separated clause.
     if (!empty($approved_clauses)) {
         if (1 === count($approved_clauses)) {
             $where[] = $approved_clauses[0];
         } else {
             $where[] = '( ' . implode(' OR ', $approved_clauses) . ' )';
         }
     }
     $order = 'ASC' == strtoupper($this->query_vars['order']) ? 'ASC' : 'DESC';
     // Disable ORDER BY with 'none', an empty array, or boolean false.
     if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
         $orderby = '';
     } elseif (!empty($this->query_vars['orderby'])) {
         $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
         $orderby_array = array();
         $found_orderby_comment_ID = false;
         foreach ($ordersby as $_key => $_value) {
             if (!$_value) {
                 continue;
             }
             if (is_int($_key)) {
                 $_orderby = $_value;
                 $_order = $order;
             } else {
                 $_orderby = $_key;
                 $_order = $_value;
             }
             if (!$found_orderby_comment_ID && 'comment_ID' === $_orderby) {
                 $found_orderby_comment_ID = true;
             }
             $parsed = $this->parse_orderby($_orderby);
             if (!$parsed) {
                 continue;
             }
             $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
         }
         // If no valid clauses were found, order by comment_date_gmt.
         if (empty($orderby_array)) {
             $orderby_array[] = "{$hqdb->comments}.comment_date_gmt {$order}";
         }
         // To ensure determinate sorting, always include a comment_ID clause.
         if (!$found_orderby_comment_ID) {
             $comment_ID_order = '';
             // Inherit order from comment_date or comment_date_gmt, if available.
             foreach ($orderby_array as $orderby_clause) {
                 if (preg_match('/comment_date(?:_gmt)*\\ (ASC|DESC)/', $orderby_clause, $match)) {
                     $comment_ID_order = $match[1];
                     break;
                 }
             }
             // If no date-related order is available, use the date from the first available clause.
             if (!$comment_ID_order) {
                 foreach ($orderby_array as $orderby_clause) {
                     if (false !== strpos('ASC', $orderby_clause)) {
                         $comment_ID_order = 'ASC';
                     } else {
                         $comment_ID_order = 'DESC';
                     }
                     break;
                 }
             }
             // Default to DESC.
             if (!$comment_ID_order) {
                 $comment_ID_order = 'DESC';
             }
             $orderby_array[] = "{$hqdb->comments}.comment_ID {$comment_ID_order}";
         }
         $orderby = implode(', ', $orderby_array);
     } else {
         $orderby = "{$hqdb->comments}.comment_date_gmt {$order}";
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     } else {
         $limits = '';
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         switch (strtolower($this->query_vars['fields'])) {
             case 'ids':
                 $fields = "{$hqdb->comments}.comment_ID";
                 break;
             default:
                 $fields = "*";
                 break;
         }
     }
     $join = '';
     $post_id = absint($this->query_vars['post_id']);
     if (!empty($post_id)) {
         $where[] = $hqdb->prepare('comment_post_ID = %d', $post_id);
     }
     // Parse comment IDs for an IN clause.
     if (!empty($this->query_vars['comment__in'])) {
         $where[] = "{$hqdb->comments}.comment_ID IN ( " . implode(',', hq_parse_id_list($this->query_vars['comment__in'])) . ' )';
     }
     // Parse comment IDs for a NOT IN clause.
     if (!empty($this->query_vars['comment__not_in'])) {
         $where[] = "{$hqdb->comments}.comment_ID NOT IN ( " . implode(',', hq_parse_id_list($this->query_vars['comment__not_in'])) . ' )';
     }
     // Parse comment post IDs for an IN clause.
     if (!empty($this->query_vars['post__in'])) {
         $where[] = 'comment_post_ID IN ( ' . implode(',', hq_parse_id_list($this->query_vars['post__in'])) . ' )';
     }
     // Parse comment post IDs for a NOT IN clause.
     if (!empty($this->query_vars['post__not_in'])) {
         $where[] = 'comment_post_ID NOT IN ( ' . implode(',', hq_parse_id_list($this->query_vars['post__not_in'])) . ' )';
     }
     if ('' !== $this->query_vars['author_email']) {
         $where[] = $hqdb->prepare('comment_author_email = %s', $this->query_vars['author_email']);
     }
     if ('' !== $this->query_vars['karma']) {
         $where[] = $hqdb->prepare('comment_karma = %d', $this->query_vars['karma']);
     }
     // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
     $raw_types = array('IN' => array_merge((array) $this->query_vars['type'], (array) $this->query_vars['type__in']), 'NOT IN' => (array) $this->query_vars['type__not_in']);
     $comment_types = array();
     foreach ($raw_types as $operator => $_raw_types) {
         $_raw_types = array_unique($_raw_types);
         foreach ($_raw_types as $type) {
             switch ($type) {
                 // An empty translates to 'all', for backward compatibility
                 case '':
                 case 'all':
                     break;
                 case 'comment':
                 case 'comments':
                     $comment_types[$operator][] = "''";
                     break;
                 case 'pings':
                     $comment_types[$operator][] = "'pingback'";
                     $comment_types[$operator][] = "'trackback'";
                     break;
                 default:
                     $comment_types[$operator][] = $hqdb->prepare('%s', $type);
                     break;
             }
         }
         if (!empty($comment_types[$operator])) {
             $types_sql = implode(', ', $comment_types[$operator]);
             $where[] = "comment_type {$operator} ({$types_sql})";
         }
     }
     if ('' !== $this->query_vars['parent']) {
         $where[] = $hqdb->prepare('comment_parent = %d', $this->query_vars['parent']);
     }
     if (is_array($this->query_vars['user_id'])) {
         $where[] = 'user_id IN (' . implode(',', array_map('absint', $this->query_vars['user_id'])) . ')';
     } elseif ('' !== $this->query_vars['user_id']) {
         $where[] = $hqdb->prepare('user_id = %d', $this->query_vars['user_id']);
     }
     if ('' !== $this->query_vars['search']) {
         $search_sql = $this->get_search_sql($this->query_vars['search'], array('comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content'));
         // Strip leading 'AND'.
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $search_sql);
     }
     // If any post-related query vars are passed, join the posts table.
     $join_posts_table = false;
     $plucked = hq_array_slice_assoc($this->query_vars, array('post_author', 'post_name', 'post_parent', 'post_status', 'post_type'));
     $post_fields = array_filter($plucked);
     if (!empty($post_fields)) {
         $join_posts_table = true;
         foreach ($post_fields as $field_name => $field_value) {
             // $field_value may be an array.
             $esses = array_fill(0, count((array) $field_value), '%s');
             $where[] = $hqdb->prepare(" {$hqdb->posts}.{$field_name} IN (" . implode(',', $esses) . ')', $field_value);
         }
     }
     // Comment author IDs for an IN clause.
     if (!empty($this->query_vars['author__in'])) {
         $where[] = 'user_id IN ( ' . implode(',', hq_parse_id_list($this->query_vars['author__in'])) . ' )';
     }
     // Comment author IDs for a NOT IN clause.
     if (!empty($this->query_vars['author__not_in'])) {
         $where[] = 'user_id NOT IN ( ' . implode(',', hq_parse_id_list($this->query_vars['author__not_in'])) . ' )';
     }
     // Post author IDs for an IN clause.
     if (!empty($this->query_vars['post_author__in'])) {
         $join_posts_table = true;
         $where[] = 'post_author IN ( ' . implode(',', hq_parse_id_list($this->query_vars['post_author__in'])) . ' )';
     }
     // Post author IDs for a NOT IN clause.
     if (!empty($this->query_vars['post_author__not_in'])) {
         $join_posts_table = true;
         $where[] = 'post_author NOT IN ( ' . implode(',', hq_parse_id_list($this->query_vars['post_author__not_in'])) . ' )';
     }
     if ($join_posts_table) {
         $join = "JOIN {$hqdb->posts} ON {$hqdb->posts}.ID = {$hqdb->comments}.comment_post_ID";
     }
     if (!empty($meta_query_clauses)) {
         $join .= $meta_query_clauses['join'];
         // Strip leading 'AND'.
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $meta_query_clauses['where']);
         if (!$this->query_vars['count']) {
             $groupby = "{$hqdb->comments}.comment_ID";
         }
     }
     $date_query = $this->query_vars['date_query'];
     if (!empty($date_query) && is_array($date_query)) {
         $date_query_object = new HQ_Date_Query($date_query, 'comment_date');
         $where[] = preg_replace('/^\\s*AND\\s*/', '', $date_query_object->get_sql());
     }
     $where = implode(' AND ', $where);
     $pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby');
     /**
      * Filter the comment query clauses.
      *
      * @since 0.0.1
      *
      * @param array            $pieces A compacted array of comment query clauses.
      * @param HQ_Comment_Query &$this  Current instance of HQ_Comment_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('comments_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     if ($where) {
         $where = 'WHERE ' . $where;
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if ($orderby) {
         $orderby = "ORDER BY {$orderby}";
     }
     $this->request = "SELECT {$fields} FROM {$hqdb->comments} {$join} {$where} {$groupby} {$orderby} {$limits}";
     if ($this->query_vars['count']) {
         return $hqdb->get_var($this->request);
     }
     if ('ids' == $this->query_vars['fields']) {
         $this->comments = $hqdb->get_col($this->request);
         return array_map('intval', $this->comments);
     }
     $results = $hqdb->get_results($this->request);
     /**
      * Filter the comment query results.
      *
      * @since 0.0.1
      *
      * @param array            $results  An array of comments.
      * @param HQ_Comment_Query &$this    Current instance of HQ_Comment_Query, passed by reference.
      */
     $comments = apply_filters_ref_array('the_comments', array($results, &$this));
     hq_cache_add($cache_key, $comments, 'comment');
     if ('*' === $fields) {
         update_comment_cache($comments);
     }
     $this->comments = $comments;
     return $this->comments;
 }
コード例 #3
0
ファイル: post.php プロジェクト: gcorral/hivequeen
/**
 * Retrieve a list of pages.
 *
 * @global hqdb $hqdb HiveQueen database abstraction object.
 *
 * @since 0.0.1
 *
 * @param array|string $args {
 *     Optional. Array or string of arguments to retrieve pages.
 *
 *     @type int          $child_of     Page ID to return child and grandchild pages of.
 *                                      Default 0, or no restriction.
 *     @type string       $sort_order   How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
 *     @type string       $sort_column  What columns to sort pages by, comma-separated. Accepts 'post_author',
 *                                      'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',
 *                                      'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'.
 *                                      'post_' can be omitted for any values that start with it.
 *                                      Default 'post_title'.
 *     @type bool         $hierarchical Whether to return pages hierarchically. Default true.
 *     @type array        $exclude      Array of page IDs to exclude. Default empty array.
 *     @type array        $include      Array of page IDs to include. Cannot be used with `$child_of`,
 *                                      `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
 *                                      Default empty array.
 *     @type string       $meta_key     Only include pages with this meta key. Default empty.
 *     @type string       $meta_value   Only include pages with this meta value. Requires `$meta_key`.
 *                                      Default empty.
 *     @type string       $authors      A comma-separated list of author IDs. Default empty.
 *     @type int          $parent       Page ID to return direct children of. `$hierarchical` must be false.
 *                                      Default -1, or no restriction.
 *     @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude.
 *                                      Default empty array.
 *     @type int          $number       The number of pages to return. Default 0, or all pages.
 *     @type int          $offset       The number of pages to skip before returning. Requires `$number`.
 *                                      Default 0.
 *     @type string       $post_type    The post type to query. Default 'page'.
 *     @type string       $post_status  A comma-separated list of post status types to include.
 *                                      Default 'publish'.
 * }
 * @return array|false List of pages matching defaults or `$args`.
 */
function get_pages($args = array())
{
    global $hqdb;
    $defaults = array('child_of' => 0, 'sort_order' => 'ASC', 'sort_column' => 'post_title', 'hierarchical' => 1, 'exclude' => array(), 'include' => array(), 'meta_key' => '', 'meta_value' => '', 'authors' => '', 'parent' => -1, 'exclude_tree' => array(), 'number' => '', 'offset' => 0, 'post_type' => 'page', 'post_status' => 'publish');
    $r = hq_parse_args($args, $defaults);
    $number = (int) $r['number'];
    $offset = (int) $r['offset'];
    $child_of = (int) $r['child_of'];
    $hierarchical = $r['hierarchical'];
    $exclude = $r['exclude'];
    $meta_key = $r['meta_key'];
    $meta_value = $r['meta_value'];
    $parent = $r['parent'];
    $post_status = $r['post_status'];
    // Make sure the post type is hierarchical.
    $hierarchical_post_types = get_post_types(array('hierarchical' => true));
    if (!in_array($r['post_type'], $hierarchical_post_types)) {
        return false;
    }
    if ($parent > 0 && !$child_of) {
        $hierarchical = false;
    }
    // Make sure we have a valid post status.
    if (!is_array($post_status)) {
        $post_status = explode(',', $post_status);
    }
    if (array_diff($post_status, get_post_stati())) {
        return false;
    }
    // $args can be whatever, only use the args defined in defaults to compute the key.
    $key = md5(serialize(hq_array_slice_assoc($r, array_keys($defaults))));
    $last_changed = hq_cache_get('last_changed', 'posts');
    if (!$last_changed) {
        $last_changed = microtime();
        hq_cache_set('last_changed', $last_changed, 'posts');
    }
    $cache_key = "get_pages:{$key}:{$last_changed}";
    if ($cache = hq_cache_get($cache_key, 'posts')) {
        // Convert to HQ_Post instances.
        $pages = array_map('get_post', $cache);
        /** This filter is documented in hq-includes/post.php */
        $pages = apply_filters('get_pages', $pages, $r);
        return $pages;
    }
    $inclusions = '';
    if (!empty($r['include'])) {
        $child_of = 0;
        //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
        $parent = -1;
        $exclude = '';
        $meta_key = '';
        $meta_value = '';
        $hierarchical = false;
        $incpages = hq_parse_id_list($r['include']);
        if (!empty($incpages)) {
            $inclusions = ' AND ID IN (' . implode(',', $incpages) . ')';
        }
    }
    $exclusions = '';
    if (!empty($exclude)) {
        $expages = hq_parse_id_list($exclude);
        if (!empty($expages)) {
            $exclusions = ' AND ID NOT IN (' . implode(',', $expages) . ')';
        }
    }
    $author_query = '';
    if (!empty($r['authors'])) {
        $post_authors = preg_split('/[\\s,]+/', $r['authors']);
        if (!empty($post_authors)) {
            foreach ($post_authors as $post_author) {
                //Do we have an author id or an author login?
                if (0 == intval($post_author)) {
                    $post_author = get_user_by('login', $post_author);
                    if (empty($post_author)) {
                        continue;
                    }
                    if (empty($post_author->ID)) {
                        continue;
                    }
                    $post_author = $post_author->ID;
                }
                if ('' == $author_query) {
                    $author_query = $hqdb->prepare(' post_author = %d ', $post_author);
                } else {
                    $author_query .= $hqdb->prepare(' OR post_author = %d ', $post_author);
                }
            }
            if ('' != $author_query) {
                $author_query = " AND ({$author_query})";
            }
        }
    }
    $join = '';
    $where = "{$exclusions} {$inclusions} ";
    if ('' !== $meta_key || '' !== $meta_value) {
        $join = " LEFT JOIN {$hqdb->postmeta} ON ( {$hqdb->posts}.ID = {$hqdb->postmeta}.post_id )";
        // meta_key and meta_value might be slashed
        $meta_key = hq_unslash($meta_key);
        $meta_value = hq_unslash($meta_value);
        if ('' !== $meta_key) {
            $where .= $hqdb->prepare(" AND {$hqdb->postmeta}.meta_key = %s", $meta_key);
        }
        if ('' !== $meta_value) {
            $where .= $hqdb->prepare(" AND {$hqdb->postmeta}.meta_value = %s", $meta_value);
        }
    }
    if (is_array($parent)) {
        $post_parent__in = implode(',', array_map('absint', (array) $parent));
        if (!empty($post_parent__in)) {
            $where .= " AND post_parent IN ({$post_parent__in})";
        }
    } elseif ($parent >= 0) {
        $where .= $hqdb->prepare(' AND post_parent = %d ', $parent);
    }
    if (1 == count($post_status)) {
        $where_post_type = $hqdb->prepare("post_type = %s AND post_status = %s", $r['post_type'], reset($post_status));
    } else {
        $post_status = implode("', '", $post_status);
        $where_post_type = $hqdb->prepare("post_type = %s AND post_status IN ('{$post_status}')", $r['post_type']);
    }
    $orderby_array = array();
    $allowed_keys = array('author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', 'ID', 'rand', 'comment_count');
    foreach (explode(',', $r['sort_column']) as $orderby) {
        $orderby = trim($orderby);
        if (!in_array($orderby, $allowed_keys)) {
            continue;
        }
        switch ($orderby) {
            case 'menu_order':
                break;
            case 'ID':
                $orderby = "{$hqdb->posts}.ID";
                break;
            case 'rand':
                $orderby = 'RAND()';
                break;
            case 'comment_count':
                $orderby = "{$hqdb->posts}.comment_count";
                break;
            default:
                if (0 === strpos($orderby, 'post_')) {
                    $orderby = "{$hqdb->posts}." . $orderby;
                } else {
                    $orderby = "{$hqdb->posts}.post_" . $orderby;
                }
        }
        $orderby_array[] = $orderby;
    }
    $sort_column = !empty($orderby_array) ? implode(',', $orderby_array) : "{$hqdb->posts}.post_title";
    $sort_order = strtoupper($r['sort_order']);
    if ('' !== $sort_order && !in_array($sort_order, array('ASC', 'DESC'))) {
        $sort_order = 'ASC';
    }
    $query = "SELECT * FROM {$hqdb->posts} {$join} WHERE ({$where_post_type}) {$where} ";
    $query .= $author_query;
    $query .= " ORDER BY " . $sort_column . " " . $sort_order;
    if (!empty($number)) {
        $query .= ' LIMIT ' . $offset . ',' . $number;
    }
    $pages = $hqdb->get_results($query);
    if (empty($pages)) {
        /** This filter is documented in hq-includes/post.php */
        $pages = apply_filters('get_pages', array(), $r);
        return $pages;
    }
    // Sanitize before caching so it'll only get done once.
    $num_pages = count($pages);
    for ($i = 0; $i < $num_pages; $i++) {
        $pages[$i] = sanitize_post($pages[$i], 'raw');
    }
    // Update cache.
    update_post_cache($pages);
    if ($child_of || $hierarchical) {
        $pages = get_page_children($child_of, $pages);
    }
    if (!empty($r['exclude_tree'])) {
        $exclude = hq_parse_id_list($r['exclude_tree']);
        foreach ($exclude as $id) {
            $children = get_page_children($id, $pages);
            foreach ($children as $child) {
                $exclude[] = $child->ID;
            }
        }
        $num_pages = count($pages);
        for ($i = 0; $i < $num_pages; $i++) {
            if (in_array($pages[$i]->ID, $exclude)) {
                unset($pages[$i]);
            }
        }
    }
    $page_structure = array();
    foreach ($pages as $page) {
        $page_structure[] = $page->ID;
    }
    hq_cache_set($cache_key, $page_structure, 'posts');
    // Convert to HQ_Post instances
    $pages = array_map('get_post', $pages);
    /**
     * Filter the retrieved list of pages.
     *
     * @since 0.0.1
     *
     * @param array $pages List of pages to retrieve.
     * @param array $r     Array of get_pages() arguments.
     */
    return apply_filters('get_pages', $pages, $r);
}