Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 38.00 KB | None | 0 0
  1.     /**
  2.      * Retrieve the posts based on query variables.
  3.      *
  4.      * There are a few filters and actions that can be used to modify the post
  5.      * database query.
  6.      *
  7.      * @since 1.5.0
  8.      * @access public
  9.      * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
  10.      *
  11.      * @return array List of posts.
  12.      */
  13.     function &get_posts() {
  14.         global $wpdb, $user_ID, $_wp_using_ext_object_cache;
  15.  
  16.         do_action_ref_array('pre_get_posts', array(&$this));
  17.  
  18.         // Shorthand.
  19.         $q = &$this->query_vars;
  20.  
  21.         $q = $this->fill_query_vars($q);
  22.  
  23.         // First let's clear some variables
  24.         $distinct = '';
  25.         $whichcat = '';
  26.         $whichauthor = '';
  27.         $whichmimetype = '';
  28.         $where = '';
  29.         $limits = '';
  30.         $join = '';
  31.         $search = '';
  32.         $groupby = '';
  33.         $fields = "$wpdb->posts.*";
  34.         $post_status_join = false;
  35.         $page = 1;
  36.  
  37.         if ( !isset($q['caller_get_posts']) )
  38.             $q['caller_get_posts'] = false;
  39.  
  40.         if ( !isset($q['suppress_filters']) )
  41.             $q['suppress_filters'] = false;
  42.  
  43.         if ( !isset($q['cache_results']) ) {
  44.             if ( $_wp_using_ext_object_cache )
  45.                 $q['cache_results'] = false;
  46.             else
  47.                 $q['cache_results'] = true;
  48.         }
  49.  
  50.         if ( !isset($q['update_post_term_cache']) )
  51.             $q['update_post_term_cache'] = true;
  52.  
  53.         if ( !isset($q['update_post_meta_cache']) )
  54.             $q['update_post_meta_cache'] = true;
  55.  
  56.         if ( !isset($q['post_type']) ) {
  57.             if ( $this->is_search )
  58.                 $q['post_type'] = 'any';
  59.             else
  60.                 $q['post_type'] = '';
  61.         }
  62.         $post_type = $q['post_type'];
  63.         if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
  64.             $q['posts_per_page'] = get_option('posts_per_page');
  65.         if ( isset($q['showposts']) && $q['showposts'] ) {
  66.             $q['showposts'] = (int) $q['showposts'];
  67.             $q['posts_per_page'] = $q['showposts'];
  68.         }
  69.         if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  70.             $q['posts_per_page'] = $q['posts_per_archive_page'];
  71.         if ( !isset($q['nopaging']) ) {
  72.             if ( $q['posts_per_page'] == -1 ) {
  73.                 $q['nopaging'] = true;
  74.             } else {
  75.                 $q['nopaging'] = false;
  76.             }
  77.         }
  78.         if ( $this->is_feed ) {
  79.             $q['posts_per_page'] = get_option('posts_per_rss');
  80.             $q['nopaging'] = false;
  81.         }
  82.         $q['posts_per_page'] = (int) $q['posts_per_page'];
  83.         if ( $q['posts_per_page'] < -1 )
  84.             $q['posts_per_page'] = abs($q['posts_per_page']);
  85.         else if ( $q['posts_per_page'] == 0 )
  86.             $q['posts_per_page'] = 1;
  87.  
  88.         if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  89.             $q['comments_per_page'] = get_option('comments_per_page');
  90.  
  91.         if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  92.             $this->is_page = true;
  93.             $this->is_home = false;
  94.             $q['page_id'] = get_option('page_on_front');
  95.         }
  96.  
  97.         if ( isset($q['page']) ) {
  98.             $q['page'] = trim($q['page'], '/');
  99.             $q['page'] = absint($q['page']);
  100.         }
  101.  
  102.         // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  103.         if ( isset($q['no_found_rows']) )
  104.             $q['no_found_rows'] = (bool) $q['no_found_rows'];
  105.         else
  106.             $q['no_found_rows'] = false;
  107.  
  108.         // If a month is specified in the querystring, load that month
  109.         if ( $q['m'] ) {
  110.             $q['m'] = '' . preg_replace('|[^0-9]|', '', $q['m']);
  111.             $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
  112.             if ( strlen($q['m']) > 5 )
  113.                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
  114.             if ( strlen($q['m']) > 7 )
  115.                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
  116.             if ( strlen($q['m']) > 9 )
  117.                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
  118.             if ( strlen($q['m']) > 11 )
  119.                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
  120.             if ( strlen($q['m']) > 13 )
  121.                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
  122.         }
  123.  
  124.         if ( '' !== $q['hour'] )
  125.             $where .= " AND HOUR($wpdb->posts.post_date)='" . $q['hour'] . "'";
  126.  
  127.         if ( '' !== $q['minute'] )
  128.             $where .= " AND MINUTE($wpdb->posts.post_date)='" . $q['minute'] . "'";
  129.  
  130.         if ( '' !== $q['second'] )
  131.             $where .= " AND SECOND($wpdb->posts.post_date)='" . $q['second'] . "'";
  132.  
  133.         if ( $q['year'] )
  134.             $where .= " AND YEAR($wpdb->posts.post_date)='" . $q['year'] . "'";
  135.  
  136.         if ( $q['monthnum'] )
  137.             $where .= " AND MONTH($wpdb->posts.post_date)='" . $q['monthnum'] . "'";
  138.  
  139.         if ( $q['day'] )
  140.             $where .= " AND DAYOFMONTH($wpdb->posts.post_date)='" . $q['day'] . "'";
  141.  
  142.         // If we've got a post_type AND its not "any" post_type.
  143.         if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  144.             foreach ( (array)$q['post_type'] as $_post_type ) {
  145.                 $ptype_obj = get_post_type_object($_post_type);
  146.                 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  147.                     continue;
  148.  
  149.                 if ( ! $ptype_obj->hierarchical || strpos($q[ $ptype_obj->query_var ], '/') === false ) {
  150.                     // Non-hierarchical post_types & parent-level-hierarchical post_types can directly use 'name'
  151.                     $q['name'] = $q[ $ptype_obj->query_var ];
  152.                 } else {
  153.                     // Hierarchical post_types will operate through the
  154.                     $q['pagename'] = $q[ $ptype_obj->query_var ];
  155.                     $q['name'] = '';
  156.                 }
  157.  
  158.                 // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  159.                 break;
  160.             } //end foreach
  161.             unset($ptype_obj);
  162.         }
  163.  
  164.         if ( '' != $q['name'] ) {
  165.             $q['name'] = sanitize_title($q['name']);
  166.             $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
  167.         } elseif ( '' != $q['pagename'] ) {
  168.             if ( isset($this->queried_object_id) ) {
  169.                 $reqpage = $this->queried_object_id;
  170.             } else {
  171.                 if ( 'page' != $q['post_type'] ) {
  172.                     foreach ( (array)$q['post_type'] as $_post_type ) {
  173.                         $ptype_obj = get_post_type_object($_post_type);
  174.                         if ( !$ptype_obj || !$ptype_obj->hierarchical )
  175.                             continue;
  176.  
  177.                         $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  178.                         if ( $reqpage )
  179.                             break;
  180.                     }
  181.                     unset($ptype_obj);
  182.                 } else {
  183.                     $reqpage = get_page_by_path($q['pagename']);
  184.                 }
  185.                 if ( !empty($reqpage) )
  186.                     $reqpage = $reqpage->ID;
  187.                 else
  188.                     $reqpage = 0;
  189.             }
  190.  
  191.             $page_for_posts = get_option('page_for_posts');
  192.             if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  193.                 $q['pagename'] = str_replace('%2F', '/', urlencode(urldecode($q['pagename'])));
  194.                 $page_paths = '/' . trim($q['pagename'], '/');
  195.                 $q['pagename'] = sanitize_title(basename($page_paths));
  196.                 $q['name'] = $q['pagename'];
  197.                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
  198.                 $reqpage_obj = get_page($reqpage);
  199.                 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  200.                     $this->is_attachment = true;
  201.                     $post_type = $q['post_type'] = 'attachment';
  202.                     $this->is_page = true;
  203.                     $q['attachment_id'] = $reqpage;
  204.                 }
  205.             }
  206.         } elseif ( '' != $q['attachment'] ) {
  207.             $q['attachment'] = str_replace('%2F', '/', urlencode(urldecode($q['attachment'])));
  208.             $attach_paths = '/' . trim($q['attachment'], '/');
  209.             $q['attachment'] = sanitize_title(basename($attach_paths));
  210.             $q['name'] = $q['attachment'];
  211.             $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
  212.         }
  213.  
  214.         if ( $q['w'] )
  215.             $where .= ' AND ' . _wp_mysql_week( "`$wpdb->posts`.`post_date`" ) . " = '" . $q['w'] . "'";
  216.  
  217.         if ( intval($q['comments_popup']) )
  218.             $q['p'] = absint($q['comments_popup']);
  219.  
  220.         // If an attachment is requested by number, let it supercede any post number.
  221.         if ( $q['attachment_id'] )
  222.             $q['p'] = absint($q['attachment_id']);
  223.  
  224.         // If a post number is specified, load that post
  225.         if ( $q['p'] ) {
  226.             $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  227.         } elseif ( $q['post__in'] ) {
  228.             $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  229.             $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  230.         } elseif ( $q['post__not_in'] ) {
  231.             $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
  232.             $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  233.         }
  234.  
  235.         if ( is_numeric($q['post_parent']) )
  236.             $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
  237.  
  238.         if ( $q['page_id'] ) {
  239.             if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  240.                 $q['p'] = $q['page_id'];
  241.                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  242.             }
  243.         }
  244.  
  245.         // If a search pattern is specified, load the posts that match
  246.         if ( !empty($q['s']) ) {
  247.             // added slashes screw with quote grouping when done early, so done later
  248.             $q['s'] = stripslashes($q['s']);
  249.             if ( !empty($q['sentence']) ) {
  250.                 $q['search_terms'] = array($q['s']);
  251.             } else {
  252.                 preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
  253.                 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
  254.             }
  255.             $n = !empty($q['exact']) ? '' : '%';
  256.             $searchand = '';
  257.             foreach( (array) $q['search_terms'] as $term ) {
  258.                 $term = addslashes_gpc($term);
  259.                 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
  260.                 $searchand = ' AND ';
  261.             }
  262.             $term = esc_sql($q['s']);
  263.             if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
  264.                 $search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
  265.  
  266.             if ( !empty($search) ) {
  267.                 $search = " AND ({$search}) ";
  268.                 if ( !is_user_logged_in() )
  269.                     $search .= " AND ($wpdb->posts.post_password = '') ";
  270.             }
  271.         }
  272.  
  273.         // Allow plugins to contextually add/remove/modify the search section of the database query
  274.         $search = apply_filters_ref_array('posts_search', array( $search, &$this ) );
  275.  
  276.         // Category stuff
  277.  
  278.         if ( empty($q['cat']) || ($q['cat'] == '0') ||
  279.                 // Bypass cat checks if fetching specific posts
  280.                 $this->is_singular ) {
  281.             $whichcat = '';
  282.         } else {
  283.             $q['cat'] = ''.urldecode($q['cat']).'';
  284.             $q['cat'] = addslashes_gpc($q['cat']);
  285.             $cat_array = preg_split('/[,\s]+/', $q['cat']);
  286.             $q['cat'] = '';
  287.             $req_cats = array();
  288.             foreach ( (array) $cat_array as $cat ) {
  289.                 $cat = intval($cat);
  290.                 $req_cats[] = $cat;
  291.                 $in = ($cat > 0);
  292.                 $cat = abs($cat);
  293.                 if ( $in ) {
  294.                     $q['category__in'][] = $cat;
  295.                     $q['category__in'] = array_merge($q['category__in'], get_term_children($cat, 'category'));
  296.                 } else {
  297.                     $q['category__not_in'][] = $cat;
  298.                     $q['category__not_in'] = array_merge($q['category__not_in'], get_term_children($cat, 'category'));
  299.                 }
  300.             }
  301.             $q['cat'] = implode(',', $req_cats);
  302.         }
  303.  
  304.         if ( !empty($q['category__in']) ) {
  305.             $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  306.             $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  307.             $include_cats = "'" . implode("', '", $q['category__in']) . "'";
  308.             $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_cats) ";
  309.         }
  310.  
  311.         if ( !empty($q['category__not_in']) ) {
  312.             $cat_string = "'" . implode("', '", $q['category__not_in']) . "'";
  313.             $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'category' AND tt.term_id IN ($cat_string) )";
  314.         }
  315.  
  316.         // Category stuff for nice URLs
  317.         if ( '' != $q['category_name'] && !$this->is_singular ) {
  318.             $q['category_name'] = implode('/', array_map('sanitize_title', explode('/', $q['category_name'])));
  319.             $reqcat = get_category_by_path($q['category_name']);
  320.             $q['category_name'] = str_replace('%2F', '/', urlencode(urldecode($q['category_name'])));
  321.             $cat_paths = '/' . trim($q['category_name'], '/');
  322.             $q['category_name'] = sanitize_title(basename($cat_paths));
  323.  
  324.             $cat_paths = '/' . trim(urldecode($q['category_name']), '/');
  325.             $q['category_name'] = sanitize_title(basename($cat_paths));
  326.             $cat_paths = explode('/', $cat_paths);
  327.             $cat_path = '';
  328.             foreach ( (array) $cat_paths as $pathdir )
  329.                 $cat_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
  330.  
  331.             //if we don't match the entire hierarchy fallback on just matching the nicename
  332.             if ( empty($reqcat) )
  333.                 $reqcat = get_category_by_path($q['category_name'], false);
  334.  
  335.             if ( !empty($reqcat) )
  336.                 $reqcat = $reqcat->term_id;
  337.             else
  338.                 $reqcat = 0;
  339.  
  340.             $q['cat'] = $reqcat;
  341.  
  342.             $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  343.             $whichcat = " AND $wpdb->term_taxonomy.taxonomy = 'category' ";
  344.             $in_cats = array($q['cat']);
  345.             $in_cats = array_merge($in_cats, get_term_children($q['cat'], 'category'));
  346.             $in_cats = "'" . implode("', '", $in_cats) . "'";
  347.             $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
  348.             $groupby = "{$wpdb->posts}.ID";
  349.         }
  350.  
  351.         // Tags
  352.         if ( '' != $q['tag'] ) {
  353.             if ( strpos($q['tag'], ',') !== false ) {
  354.                 $tags = preg_split('/[,\s]+/', $q['tag']);
  355.                 foreach ( (array) $tags as $tag ) {
  356.                     $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  357.                     $q['tag_slug__in'][] = $tag;
  358.                 }
  359.             } else if ( preg_match('/[+\s]+/', $q['tag']) || !empty($q['cat']) ) {
  360.                 $tags = preg_split('/[+\s]+/', $q['tag']);
  361.                 foreach ( (array) $tags as $tag ) {
  362.                     $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  363.                     $q['tag_slug__and'][] = $tag;
  364.                 }
  365.             } else {
  366.                 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  367.                 $q['tag_slug__in'][] = $q['tag'];
  368.             }
  369.         }
  370.  
  371.         if ( !empty($q['category__in']) || !empty($q['meta_key']) || !empty($q['tag__in']) || !empty($q['tag_slug__in']) ) {
  372.             $groupby = "{$wpdb->posts}.ID";
  373.         }
  374.  
  375.         if ( !empty($q['tag__in']) && empty($q['cat']) ) {
  376.             $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
  377.             $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  378.             $include_tags = "'" . implode("', '", $q['tag__in']) . "'";
  379.             $whichcat .= " AND $wpdb->term_taxonomy.term_id IN ($include_tags) ";
  380.             $reqtag = term_exists( $q['tag__in'][0], 'post_tag' );
  381.             if ( !empty($reqtag) )
  382.                 $q['tag_id'] = $reqtag['term_id'];
  383.         }
  384.  
  385.         if ( !empty($q['tag_slug__in']) && empty($q['cat']) ) {
  386.             $join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) ";
  387.             $whichcat .= " AND $wpdb->term_taxonomy.taxonomy = 'post_tag' ";
  388.             $include_tags = "'" . implode("', '", $q['tag_slug__in']) . "'";
  389.             $whichcat .= " AND $wpdb->terms.slug IN ($include_tags) ";
  390.             $reqtag = get_term_by( 'slug', $q['tag_slug__in'][0], 'post_tag' );
  391.             if ( !empty($reqtag) )
  392.                 $q['tag_id'] = $reqtag->term_id;
  393.         }
  394.  
  395.         if ( !empty($q['tag__not_in']) ) {
  396.             $tag_string = "'" . implode("', '", $q['tag__not_in']) . "'";
  397.             $whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = 'post_tag' AND tt.term_id IN ($tag_string) )";
  398.         }
  399.  
  400.         // Tag and slug intersections.
  401.         $intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
  402.         $tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
  403.         foreach ( $intersections as $item => $taxonomy ) {
  404.             if ( empty($q[$item]) ) continue;
  405.             if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
  406.  
  407.             if ( $item != 'category__and' ) {
  408.                 $reqtag = term_exists( $q[$item][0], 'post_tag' );
  409.                 if ( !empty($reqtag) )
  410.                     $q['tag_id'] = $reqtag['term_id'];
  411.             }
  412.  
  413.             if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
  414.                 $taxonomy_field = 'slug';
  415.             else
  416.                 $taxonomy_field = 'term_id';
  417.  
  418.             $q[$item] = array_unique($q[$item]);
  419.             $tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
  420.             $tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
  421.             if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
  422.                 $tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
  423.             }
  424.             $post_ids = $wpdb->get_col($tsql);
  425.  
  426.             if ( count($post_ids) )
  427.                 $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  428.             else {
  429.                 $whichcat = " AND 0 = 1";
  430.                 break;
  431.             }
  432.         }
  433.  
  434.         // Taxonomies
  435.         if ( $this->is_tax ) {
  436.             if ( '' != $q['taxonomy'] ) {
  437.                 $taxonomy = $q['taxonomy'];
  438.                 $tt[$taxonomy] = $q['term'];
  439.             } else {
  440.                 foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
  441.                     if ( $t->query_var && '' != $q[$t->query_var] ) {
  442.                         $tt[$taxonomy] = $q[$t->query_var];
  443.                         break;
  444.                     }
  445.                 }
  446.             }
  447.  
  448.             $terms = get_terms($taxonomy, array('slug' => $tt[$taxonomy], 'hide_empty' => !is_taxonomy_hierarchical($taxonomy)));
  449.  
  450.             if ( is_wp_error($terms) || empty($terms) ) {
  451.                 $whichcat = " AND 0 ";
  452.             } else {
  453.                 foreach ( $terms as $term ) {
  454.                     $term_ids[] = $term->term_id;
  455.                     if ( is_taxonomy_hierarchical($taxonomy) ) {
  456.                         $children = get_term_children($term->term_id, $taxonomy);
  457.                         $term_ids = array_merge($term_ids, $children);
  458.                     }
  459.                 }
  460.                 $post_ids = get_objects_in_term($term_ids, $taxonomy);
  461.                 if ( !is_wp_error($post_ids) && !empty($post_ids) ) {
  462.                     $whichcat .= " AND $wpdb->posts.ID IN (" . implode(', ', $post_ids) . ") ";
  463.                     if ( empty($post_type) ) {
  464.                         $post_type = 'any';
  465.                         $post_status_join = true;
  466.                     } elseif ( in_array('attachment', (array)$post_type) ) {
  467.                         $post_status_join = true;
  468.                     }
  469.                 } else {
  470.                     $whichcat = " AND 0 ";
  471.                 }
  472.             }
  473.         }
  474.  
  475.         // Author/user stuff
  476.  
  477.         if ( empty($q['author']) || ($q['author'] == '0') ) {
  478.             $whichauthor = '';
  479.         } else {
  480.             $q['author'] = (string)urldecode($q['author']);
  481.             $q['author'] = addslashes_gpc($q['author']);
  482.             if ( strpos($q['author'], '-') !== false ) {
  483.                 $eq = '!=';
  484.                 $andor = 'AND';
  485.                 $q['author'] = explode('-', $q['author']);
  486.                 $q['author'] = (string)absint($q['author'][1]);
  487.             } else {
  488.                 $eq = '=';
  489.                 $andor = 'OR';
  490.             }
  491.             $author_array = preg_split('/[,\s]+/', $q['author']);
  492.             $_author_array = array();
  493.             foreach ( $author_array as $key => $_author )
  494.                 $_author_array[] = "$wpdb->posts.post_author " . $eq . ' ' . absint($_author);
  495.             $whichauthor .= ' AND (' . implode(" $andor ", $_author_array) . ')';
  496.             unset($author_array, $_author_array);
  497.         }
  498.  
  499.         // Author stuff for nice URLs
  500.  
  501.         if ( '' != $q['author_name'] ) {
  502.             if ( strpos($q['author_name'], '/') !== false ) {
  503.                 $q['author_name'] = explode('/', $q['author_name']);
  504.                 if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  505.                     $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  506.                 } else {
  507.                     $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailling slash
  508.                 }
  509.             }
  510.             $q['author_name'] = sanitize_title($q['author_name']);
  511.             $q['author'] = get_user_by('slug', $q['author_name']);
  512.             if ( $q['author'] )
  513.                 $q['author'] = $q['author']->ID;
  514.             $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
  515.         }
  516.  
  517.         // MIME-Type stuff for attachment browsing
  518.  
  519.         if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
  520.             $table_alias = $post_status_join ? $wpdb->posts : '';
  521.             $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
  522.         }
  523.  
  524.         $where .= $search . $whichcat . $whichauthor . $whichmimetype;
  525.  
  526.         if ( empty($q['order']) || ((strtoupper($q['order']) != 'ASC') && (strtoupper($q['order']) != 'DESC')) )
  527.             $q['order'] = 'DESC';
  528.  
  529.         // Order by
  530.         if ( empty($q['orderby']) ) {
  531.             $q['orderby'] = "$wpdb->posts.post_date " . $q['order'];
  532.         } elseif ( 'none' == $q['orderby'] ) {
  533.             $q['orderby'] = '';
  534.         } else {
  535.             // Used to filter values
  536.             $allowed_keys = array('author', 'date', 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand', 'comment_count');
  537.             if ( !empty($q['meta_key']) ) {
  538.                 $allowed_keys[] = $q['meta_key'];
  539.                 $allowed_keys[] = 'meta_value';
  540.                 $allowed_keys[] = 'meta_value_num';
  541.             }
  542.             $q['orderby'] = urldecode($q['orderby']);
  543.             $q['orderby'] = addslashes_gpc($q['orderby']);
  544.             $orderby_array = explode(' ', $q['orderby']);
  545.             $q['orderby'] = '';
  546.  
  547.             foreach ( $orderby_array as $i => $orderby ) {
  548.                 // Only allow certain values for safety
  549.                 if ( ! in_array($orderby, $allowed_keys) )
  550.                     continue;
  551.  
  552.                 switch ( $orderby ) {
  553.                     case 'menu_order':
  554.                         break;
  555.                     case 'ID':
  556.                         $orderby = "$wpdb->posts.ID";
  557.                         break;
  558.                     case 'rand':
  559.                         $orderby = 'RAND()';
  560.                         break;
  561.                     case $q['meta_key']:
  562.                     case 'meta_value':
  563.                         $orderby = "$wpdb->postmeta.meta_value";
  564.                         break;
  565.                     case 'meta_value_num':
  566.                         $orderby = "$wpdb->postmeta.meta_value+0";
  567.                         break;
  568.                     case 'comment_count':
  569.                         $orderby = "$wpdb->posts.comment_count";
  570.                         break;
  571.                     default:
  572.                         $orderby = "$wpdb->posts.post_" . $orderby;
  573.                 }
  574.  
  575.                 $q['orderby'] .= (($i == 0) ? '' : ',') . $orderby;
  576.             }
  577.  
  578.             // append ASC or DESC at the end
  579.             if ( !empty($q['orderby']))
  580.                 $q['orderby'] .= " {$q['order']}";
  581.  
  582.             if ( empty($q['orderby']) )
  583.                 $q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
  584.         }
  585.  
  586.         if ( is_array($post_type) ) {
  587.             $post_type_cap = 'multiple_post_type';
  588.         } else {
  589.             $post_type_object = get_post_type_object ( $post_type );
  590.             if ( !empty($post_type_object) )
  591.                 $post_type_cap = $post_type_object->capability_type;
  592.             else
  593.                 $post_type_cap = $post_type;
  594.         }
  595.  
  596.         $exclude_post_types = '';
  597.         $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  598.         if ( ! empty( $in_search_post_types ) )
  599.             $exclude_post_types .= $wpdb->prepare(" AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')");
  600.  
  601.         if ( 'any' == $post_type ) {
  602.             $where .= $exclude_post_types;
  603.         } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  604.             $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
  605.         } elseif ( ! empty( $post_type ) ) {
  606.             $where .= " AND $wpdb->posts.post_type = '$post_type'";
  607.             $post_type_object = get_post_type_object ( $post_type );
  608.         } elseif ( $this->is_attachment ) {
  609.             $where .= " AND $wpdb->posts.post_type = 'attachment'";
  610.             $post_type_object = get_post_type_object ( 'attachment' );
  611.         } elseif ( $this->is_page ) {
  612.             $where .= " AND $wpdb->posts.post_type = 'page'";
  613.             $post_type_object = get_post_type_object ( 'page' );
  614.         } else {
  615.             $where .= " AND $wpdb->posts.post_type = 'post'";
  616.             $post_type_object = get_post_type_object ( 'post' );
  617.         }
  618.  
  619.         if ( !empty($post_type_object) ) {
  620.             $post_type_cap = $post_type_object->capability_type;
  621.             $edit_cap = $post_type_object->cap->edit_post;
  622.             $read_cap = $post_type_object->cap->read_post;
  623.             $edit_others_cap = $post_type_object->cap->edit_others_posts;
  624.             $read_private_cap = $post_type_object->cap->read_private_posts;
  625.         } else {
  626.             $edit_cap = 'edit_' . $post_type_cap;
  627.             $read_cap = 'read_' . $post_type_cap;
  628.             $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  629.             $read_private_cap = 'read_private_' . $post_type_cap . 's';
  630.         }
  631.  
  632.         if ( isset($q['post_status']) && '' != $q['post_status'] ) {
  633.             $statuswheres = array();
  634.             $q_status = explode(',', $q['post_status']);
  635.             $r_status = array();
  636.             $p_status = array();
  637.             $e_status = array();
  638.             if ( $q['post_status'] == 'any' ) {
  639.                 foreach ( get_post_stati( array('exclude_from_search' => true) ) as $status )
  640.                     $e_status[] = "$wpdb->posts.post_status <> '$status'";
  641.             } else {
  642.                 foreach ( get_post_stati() as $status ) {
  643.                     if ( in_array( $status, $q_status ) ) {
  644.                         if ( 'private' == $status )
  645.                             $p_status[] = "$wpdb->posts.post_status = '$status'";
  646.                         else
  647.                             $r_status[] = "$wpdb->posts.post_status = '$status'";
  648.                     }
  649.                 }
  650.             }
  651.  
  652.             if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  653.                 $r_status = array_merge($r_status, $p_status);
  654.                 unset($p_status);
  655.             }
  656.  
  657.             if ( !empty($e_status) ) {
  658.                 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  659.             }
  660.             if ( !empty($r_status) ) {
  661.                 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
  662.                     $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
  663.                 else
  664.                     $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  665.             }
  666.             if ( !empty($p_status) ) {
  667.                 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
  668.                     $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
  669.                 else
  670.                     $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  671.             }
  672.             if ( $post_status_join ) {
  673.                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
  674.                 foreach ( $statuswheres as $index => $statuswhere )
  675.                     $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
  676.             }
  677.             foreach ( $statuswheres as $statuswhere )
  678.                 $where .= " AND $statuswhere";
  679.         } elseif ( !$this->is_singular ) {
  680.             $where .= " AND ($wpdb->posts.post_status = 'publish'";
  681.  
  682.             // Add public states.
  683.             $public_states = get_post_stati( array('public' => true) );
  684.             foreach ( (array) $public_states as $state ) {
  685.                 if ( 'publish' == $state ) // Publish is hard-coded above.
  686.                     continue;
  687.                 $where .= " OR $wpdb->posts.post_status = '$state'";
  688.             }
  689.  
  690.             if ( is_admin() ) {
  691.                 // Add protected states that should show in the admin all list.
  692.                 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  693.                 foreach ( (array) $admin_all_states as $state )
  694.                     $where .= " OR $wpdb->posts.post_status = '$state'";
  695.             }
  696.  
  697.             if ( is_user_logged_in() ) {
  698.                 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  699.                 $private_states = get_post_stati( array('private' => true) );
  700.                 foreach ( (array) $private_states as $state )
  701.                     $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = '$state'";
  702.             }
  703.  
  704.             $where .= ')';
  705.         }
  706.  
  707.         // postmeta queries
  708.         if ( ! empty($q['meta_key']) || ! empty($q['meta_value']) )
  709.             $join .= " JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
  710.         if ( ! empty($q['meta_key']) )
  711.             $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s ", $q['meta_key']);
  712.         if ( ! empty($q['meta_value']) ) {
  713.             if ( empty($q['meta_compare']) || ! in_array($q['meta_compare'], array('=', '!=', '>', '>=', '<', '<=')) )
  714.                 $q['meta_compare'] = '=';
  715.  
  716.             $where .= $wpdb->prepare("AND $wpdb->postmeta.meta_value {$q['meta_compare']} %s ", $q['meta_value']);
  717.         }
  718.  
  719.         // Apply filters on where and join prior to paging so that any
  720.         // manipulations to them are reflected in the paging by day queries.
  721.         if ( !$q['suppress_filters'] ) {
  722.             $where = apply_filters_ref_array('posts_where', array( $where, &$this ) );
  723.             $join = apply_filters_ref_array('posts_join', array( $join, &$this ) );
  724.         }
  725.  
  726.         // Paging
  727.         if ( empty($q['nopaging']) && !$this->is_singular ) {
  728.             $page = absint($q['paged']);
  729.             if ( empty($page) )
  730.                 $page = 1;
  731.  
  732.             if ( empty($q['offset']) ) {
  733.                 $pgstrt = '';
  734.                 $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
  735.                 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  736.             } else { // we're ignoring $page and using 'offset'
  737.                 $q['offset'] = absint($q['offset']);
  738.                 $pgstrt = $q['offset'] . ', ';
  739.                 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  740.             }
  741.         }
  742.  
  743.         // Comments feeds
  744.         if ( $this->is_comment_feed && ( $this->is_archive || $this->is_search || !$this->is_singular ) ) {
  745.             if ( $this->is_archive || $this->is_search ) {
  746.                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
  747.                 $cwhere = "WHERE comment_approved = '1' $where";
  748.                 $cgroupby = "$wpdb->comments.comment_id";
  749.             } else { // Other non singular e.g. front
  750.                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
  751.                 $cwhere = "WHERE post_status = 'publish' AND comment_approved = '1'";
  752.                 $cgroupby = '';
  753.             }
  754.  
  755.             if ( !$q['suppress_filters'] ) {
  756.                 $cjoin = apply_filters_ref_array('comment_feed_join', array( $cjoin, &$this ) );
  757.                 $cwhere = apply_filters_ref_array('comment_feed_where', array( $cwhere, &$this ) );
  758.                 $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( $cgroupby, &$this ) );
  759.                 $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  760.                 $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  761.             }
  762.             $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  763.             $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  764.  
  765.             $this->comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
  766.             $this->comment_count = count($this->comments);
  767.  
  768.             $post_ids = array();
  769.  
  770.             foreach ( $this->comments as $comment )
  771.                 $post_ids[] = (int) $comment->comment_post_ID;
  772.  
  773.             $post_ids = join(',', $post_ids);
  774.             $join = '';
  775.             if ( $post_ids )
  776.                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
  777.             else
  778.                 $where = "AND 0";
  779.         }
  780.  
  781.         $orderby = $q['orderby'];
  782.  
  783.         // Apply post-paging filters on where and join.  Only plugins that
  784.         // manipulate paging queries should use these hooks.
  785.         if ( !$q['suppress_filters'] ) {
  786.             $where      = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  787.             $groupby    = apply_filters_ref_array( 'posts_groupby',     array( $groupby, &$this ) );
  788.             $join       = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
  789.             $orderby    = apply_filters_ref_array( 'posts_orderby',     array( $orderby, &$this ) );
  790.             $distinct   = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
  791.             $limits     = apply_filters_ref_array( 'post_limits',       array( $limits, &$this ) );
  792.             $fields     = apply_filters_ref_array( 'posts_fields',      array( $fields, &$this ) );
  793.         }
  794.  
  795.         // Announce current selection parameters.  For use by caching plugins.
  796.         do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  797.  
  798.         // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
  799.         if ( !$q['suppress_filters'] ) {
  800.             $where      = apply_filters_ref_array( 'posts_where_request',   array( $where, &$this ) );
  801.             $groupby    = apply_filters_ref_array( 'posts_groupby_request',     array( $groupby, &$this ) );
  802.             $join       = apply_filters_ref_array( 'posts_join_request',    array( $join, &$this ) );
  803.             $orderby    = apply_filters_ref_array( 'posts_orderby_request',     array( $orderby, &$this ) );
  804.             $distinct   = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
  805.             $fields     = apply_filters_ref_array( 'posts_fields_request',      array( $fields, &$this ) );
  806.             $limits     = apply_filters_ref_array( 'post_limits_request',       array( $limits, &$this ) );
  807.         }
  808.  
  809.         if ( ! empty($groupby) )
  810.             $groupby = 'GROUP BY ' . $groupby;
  811.         if ( !empty( $orderby ) )
  812.             $orderby = 'ORDER BY ' . $orderby;
  813.         $found_rows = '';
  814.         if ( !$q['no_found_rows'] && !empty($limits) )
  815.             $found_rows = 'SQL_CALC_FOUND_ROWS';
  816.  
  817.         $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
  818.         if ( !$q['suppress_filters'] )
  819.             $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
  820.  
  821.         $this->posts = $wpdb->get_results($this->request);
  822.         // Raw results filter.  Prior to status checks.
  823.         if ( !$q['suppress_filters'] )
  824.             $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
  825.  
  826.         if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  827.             $cjoin = apply_filters_ref_array('comment_feed_join', array( '', &$this ) );
  828.             $cwhere = apply_filters_ref_array('comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  829.             $cgroupby = apply_filters_ref_array('comment_feed_groupby', array( '', &$this ) );
  830.             $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  831.             $corderby = apply_filters_ref_array('comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  832.             $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  833.             $climits = apply_filters_ref_array('comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  834.             $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
  835.             $this->comments = $wpdb->get_results($comments_request);
  836.             $this->comment_count = count($this->comments);
  837.         }
  838.  
  839.         if ( !$q['no_found_rows'] && !empty($limits) ) {
  840.             $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
  841.             $this->found_posts = $wpdb->get_var( $found_posts_query );
  842.             $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  843.             $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
  844.         }
  845.  
  846.         // Check post status to determine if post should be displayed.
  847.         if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  848.             $status = get_post_status($this->posts[0]);
  849.             $post_status_obj = get_post_status_object($status);
  850.             //$type = get_post_type($this->posts[0]);
  851.             if ( !$post_status_obj->public ) {
  852.                 if ( ! is_user_logged_in() ) {
  853.                     // User must be logged in to view unpublished posts.
  854.                     $this->posts = array();
  855.                 } else {
  856.                     if  ( $post_status_obj->protected ) {
  857.                         // User must have edit permissions on the draft to preview.
  858.                         if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  859.                             $this->posts = array();
  860.                         } else {
  861.                             $this->is_preview = true;
  862.                             if ( 'future' != $status )
  863.                                 $this->posts[0]->post_date = current_time('mysql');
  864.                         }
  865.                     } elseif ( $post_status_obj->private ) {
  866.                         if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  867.                             $this->posts = array();
  868.                     } else {
  869.                         $this->posts = array();
  870.                     }
  871.                 }
  872.             }
  873.  
  874.             if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
  875.                 $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
  876.         }
  877.  
  878.         // Put sticky posts at the top of the posts array
  879.         $sticky_posts = get_option('sticky_posts');
  880.         if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['caller_get_posts'] ) {
  881.             $num_posts = count($this->posts);
  882.             $sticky_offset = 0;
  883.             // Loop over posts and relocate stickies to the front.
  884.             for ( $i = 0; $i < $num_posts; $i++ ) {
  885.                 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  886.                     $sticky_post = $this->posts[$i];
  887.                     // Remove sticky from current position
  888.                     array_splice($this->posts, $i, 1);
  889.                     // Move to front, after other stickies
  890.                     array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  891.                     // Increment the sticky offset.  The next sticky will be placed at this offset.
  892.                     $sticky_offset++;
  893.                     // Remove post from sticky posts array
  894.                     $offset = array_search($sticky_post->ID, $sticky_posts);
  895.                     unset( $sticky_posts[$offset] );
  896.                 }
  897.             }
  898.  
  899.             // If any posts have been excluded specifically, Ignore those that are sticky.
  900.             if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  901.                 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  902.  
  903.             // Fetch sticky posts that weren't in the query results
  904.             if ( !empty($sticky_posts) ) {
  905.                 $stickies__in = implode(',', array_map( 'absint', $sticky_posts ));
  906.                 // honor post type(s) if not set to any
  907.                 $stickies_where = '';
  908.                 if ( 'any' != $post_type && '' != $post_type ) {
  909.                     if ( is_array( $post_type ) ) {
  910.                         $post_types = join( "', '", $post_type );
  911.                     } else {
  912.                         $post_types = $post_type;
  913.                     }
  914.                     $stickies_where = "AND $wpdb->posts.post_type IN ('" . $post_types . "')";
  915.                 }
  916.  
  917.                 $stickies = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.ID IN ($stickies__in) $stickies_where" );
  918.                 foreach ( $stickies as $sticky_post ) {
  919.                     // Ignore sticky posts the current user cannot read or are not published.
  920.                     if ( 'publish' != $sticky_post->post_status )
  921.                         continue;
  922.                     array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  923.                     $sticky_offset++;
  924.                 }
  925.             }
  926.         }
  927.  
  928.         if ( !$q['suppress_filters'] )
  929.             $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
  930.  
  931.         $this->post_count = count($this->posts);
  932.  
  933.         // Sanitize before caching so it'll only get done once
  934.         for ( $i = 0; $i < $this->post_count; $i++ ) {
  935.             $this->posts[$i] = sanitize_post($this->posts[$i], 'raw');
  936.         }
  937.  
  938.         if ( $q['cache_results'] )
  939.             update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  940.  
  941.         if ( $this->post_count > 0 ) {
  942.             $this->post = $this->posts[0];
  943.         }
  944.  
  945.         return $this->posts;
  946.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement