| Conditions | 291 |
| Paths | 0 |
| Total Lines | 1365 |
| Code Lines | 648 |
| Lines | 295 |
| Ratio | 21.61 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 1667 | public function get_posts() { |
||
| 1668 | global $wpdb; |
||
| 1669 | |||
| 1670 | $this->parse_query(); |
||
| 1671 | |||
| 1672 | /** |
||
| 1673 | * Fires after the query variable object is created, but before the actual query is run. |
||
| 1674 | * |
||
| 1675 | * Note: If using conditional tags, use the method versions within the passed instance |
||
| 1676 | * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions |
||
| 1677 | * like is_main_query() test against the global $wp_query instance, not the passed one. |
||
| 1678 | * |
||
| 1679 | * @since 2.0.0 |
||
| 1680 | * |
||
| 1681 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 1682 | */ |
||
| 1683 | do_action_ref_array( 'pre_get_posts', array( &$this ) ); |
||
| 1684 | |||
| 1685 | // Shorthand. |
||
| 1686 | $q = &$this->query_vars; |
||
| 1687 | |||
| 1688 | // Fill again in case pre_get_posts unset some vars. |
||
| 1689 | $q = $this->fill_query_vars($q); |
||
| 1690 | |||
| 1691 | // Parse meta query |
||
| 1692 | $this->meta_query = new WP_Meta_Query(); |
||
| 1693 | $this->meta_query->parse_query_vars( $q ); |
||
| 1694 | |||
| 1695 | // Set a flag if a pre_get_posts hook changed the query vars. |
||
| 1696 | $hash = md5( serialize( $this->query_vars ) ); |
||
| 1697 | if ( $hash != $this->query_vars_hash ) { |
||
| 1698 | $this->query_vars_changed = true; |
||
| 1699 | $this->query_vars_hash = $hash; |
||
| 1700 | } |
||
| 1701 | unset($hash); |
||
| 1702 | |||
| 1703 | // First let's clear some variables |
||
| 1704 | $distinct = ''; |
||
| 1705 | $whichauthor = ''; |
||
| 1706 | $whichmimetype = ''; |
||
| 1707 | $where = ''; |
||
| 1708 | $limits = ''; |
||
| 1709 | $join = ''; |
||
| 1710 | $search = ''; |
||
| 1711 | $groupby = ''; |
||
| 1712 | $post_status_join = false; |
||
| 1713 | $page = 1; |
||
| 1714 | |||
| 1715 | if ( isset( $q['caller_get_posts'] ) ) { |
||
| 1716 | _deprecated_argument( 'WP_Query', '3.1.0', |
||
| 1717 | /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */ |
||
| 1718 | sprintf( __( '%1$s is deprecated. Use %2$s instead.' ), |
||
| 1719 | '<code>caller_get_posts</code>', |
||
| 1720 | '<code>ignore_sticky_posts</code>' |
||
| 1721 | ) |
||
| 1722 | ); |
||
| 1723 | |||
| 1724 | if ( ! isset( $q['ignore_sticky_posts'] ) ) { |
||
| 1725 | $q['ignore_sticky_posts'] = $q['caller_get_posts']; |
||
| 1726 | } |
||
| 1727 | } |
||
| 1728 | |||
| 1729 | if ( !isset( $q['ignore_sticky_posts'] ) ) |
||
| 1730 | $q['ignore_sticky_posts'] = false; |
||
| 1731 | |||
| 1732 | if ( !isset($q['suppress_filters']) ) |
||
| 1733 | $q['suppress_filters'] = false; |
||
| 1734 | |||
| 1735 | if ( !isset($q['cache_results']) ) { |
||
| 1736 | if ( wp_using_ext_object_cache() ) |
||
| 1737 | $q['cache_results'] = false; |
||
| 1738 | else |
||
| 1739 | $q['cache_results'] = true; |
||
| 1740 | } |
||
| 1741 | |||
| 1742 | if ( !isset($q['update_post_term_cache']) ) |
||
| 1743 | $q['update_post_term_cache'] = true; |
||
| 1744 | |||
| 1745 | if ( ! isset( $q['lazy_load_term_meta'] ) ) { |
||
| 1746 | $q['lazy_load_term_meta'] = $q['update_post_term_cache']; |
||
| 1747 | } |
||
| 1748 | |||
| 1749 | if ( !isset($q['update_post_meta_cache']) ) |
||
| 1750 | $q['update_post_meta_cache'] = true; |
||
| 1751 | |||
| 1752 | if ( !isset($q['post_type']) ) { |
||
| 1753 | if ( $this->is_search ) |
||
| 1754 | $q['post_type'] = 'any'; |
||
| 1755 | else |
||
| 1756 | $q['post_type'] = ''; |
||
| 1757 | } |
||
| 1758 | $post_type = $q['post_type']; |
||
| 1759 | if ( empty( $q['posts_per_page'] ) ) { |
||
| 1760 | $q['posts_per_page'] = get_option( 'posts_per_page' ); |
||
| 1761 | } |
||
| 1762 | if ( isset($q['showposts']) && $q['showposts'] ) { |
||
| 1763 | $q['showposts'] = (int) $q['showposts']; |
||
| 1764 | $q['posts_per_page'] = $q['showposts']; |
||
| 1765 | } |
||
| 1766 | if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) |
||
| 1767 | $q['posts_per_page'] = $q['posts_per_archive_page']; |
||
| 1768 | if ( !isset($q['nopaging']) ) { |
||
| 1769 | if ( $q['posts_per_page'] == -1 ) { |
||
| 1770 | $q['nopaging'] = true; |
||
| 1771 | } else { |
||
| 1772 | $q['nopaging'] = false; |
||
| 1773 | } |
||
| 1774 | } |
||
| 1775 | |||
| 1776 | if ( $this->is_feed ) { |
||
| 1777 | // This overrides posts_per_page. |
||
| 1778 | if ( ! empty( $q['posts_per_rss'] ) ) { |
||
| 1779 | $q['posts_per_page'] = $q['posts_per_rss']; |
||
| 1780 | } else { |
||
| 1781 | $q['posts_per_page'] = get_option( 'posts_per_rss' ); |
||
| 1782 | } |
||
| 1783 | $q['nopaging'] = false; |
||
| 1784 | } |
||
| 1785 | $q['posts_per_page'] = (int) $q['posts_per_page']; |
||
| 1786 | if ( $q['posts_per_page'] < -1 ) |
||
| 1787 | $q['posts_per_page'] = abs($q['posts_per_page']); |
||
| 1788 | elseif ( $q['posts_per_page'] == 0 ) |
||
| 1789 | $q['posts_per_page'] = 1; |
||
| 1790 | |||
| 1791 | if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 ) |
||
| 1792 | $q['comments_per_page'] = get_option('comments_per_page'); |
||
| 1793 | |||
| 1794 | if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) { |
||
| 1795 | $this->is_page = true; |
||
| 1796 | $this->is_home = false; |
||
| 1797 | $q['page_id'] = get_option('page_on_front'); |
||
| 1798 | } |
||
| 1799 | |||
| 1800 | if ( isset($q['page']) ) { |
||
| 1801 | $q['page'] = trim($q['page'], '/'); |
||
| 1802 | $q['page'] = absint($q['page']); |
||
| 1803 | } |
||
| 1804 | |||
| 1805 | // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present. |
||
| 1806 | if ( isset($q['no_found_rows']) ) |
||
| 1807 | $q['no_found_rows'] = (bool) $q['no_found_rows']; |
||
| 1808 | else |
||
| 1809 | $q['no_found_rows'] = false; |
||
| 1810 | |||
| 1811 | switch ( $q['fields'] ) { |
||
| 1812 | case 'ids': |
||
| 1813 | $fields = "{$wpdb->posts}.ID"; |
||
| 1814 | break; |
||
| 1815 | case 'id=>parent': |
||
| 1816 | $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent"; |
||
| 1817 | break; |
||
| 1818 | default: |
||
| 1819 | $fields = "{$wpdb->posts}.*"; |
||
| 1820 | } |
||
| 1821 | |||
| 1822 | if ( '' !== $q['menu_order'] ) { |
||
| 1823 | $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; |
||
| 1824 | } |
||
| 1825 | // The "m" parameter is meant for months but accepts datetimes of varying specificity |
||
| 1826 | if ( $q['m'] ) { |
||
| 1827 | $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4); |
||
| 1828 | View Code Duplication | if ( strlen($q['m']) > 5 ) { |
|
| 1829 | $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2); |
||
| 1830 | } |
||
| 1831 | View Code Duplication | if ( strlen($q['m']) > 7 ) { |
|
| 1832 | $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2); |
||
| 1833 | } |
||
| 1834 | View Code Duplication | if ( strlen($q['m']) > 9 ) { |
|
| 1835 | $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2); |
||
| 1836 | } |
||
| 1837 | View Code Duplication | if ( strlen($q['m']) > 11 ) { |
|
| 1838 | $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2); |
||
| 1839 | } |
||
| 1840 | View Code Duplication | if ( strlen($q['m']) > 13 ) { |
|
| 1841 | $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2); |
||
| 1842 | } |
||
| 1843 | } |
||
| 1844 | |||
| 1845 | // Handle the other individual date parameters |
||
| 1846 | $date_parameters = array(); |
||
| 1847 | |||
| 1848 | if ( '' !== $q['hour'] ) |
||
| 1849 | $date_parameters['hour'] = $q['hour']; |
||
| 1850 | |||
| 1851 | if ( '' !== $q['minute'] ) |
||
| 1852 | $date_parameters['minute'] = $q['minute']; |
||
| 1853 | |||
| 1854 | if ( '' !== $q['second'] ) |
||
| 1855 | $date_parameters['second'] = $q['second']; |
||
| 1856 | |||
| 1857 | if ( $q['year'] ) |
||
| 1858 | $date_parameters['year'] = $q['year']; |
||
| 1859 | |||
| 1860 | if ( $q['monthnum'] ) |
||
| 1861 | $date_parameters['monthnum'] = $q['monthnum']; |
||
| 1862 | |||
| 1863 | if ( $q['w'] ) |
||
| 1864 | $date_parameters['week'] = $q['w']; |
||
| 1865 | |||
| 1866 | if ( $q['day'] ) |
||
| 1867 | $date_parameters['day'] = $q['day']; |
||
| 1868 | |||
| 1869 | if ( $date_parameters ) { |
||
| 1870 | $date_query = new WP_Date_Query( array( $date_parameters ) ); |
||
| 1871 | $where .= $date_query->get_sql(); |
||
| 1872 | } |
||
| 1873 | unset( $date_parameters, $date_query ); |
||
| 1874 | |||
| 1875 | // Handle complex date queries |
||
| 1876 | if ( ! empty( $q['date_query'] ) ) { |
||
| 1877 | $this->date_query = new WP_Date_Query( $q['date_query'] ); |
||
| 1878 | $where .= $this->date_query->get_sql(); |
||
| 1879 | } |
||
| 1880 | |||
| 1881 | |||
| 1882 | // If we've got a post_type AND it's not "any" post_type. |
||
| 1883 | if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) { |
||
| 1884 | foreach ( (array)$q['post_type'] as $_post_type ) { |
||
| 1885 | $ptype_obj = get_post_type_object($_post_type); |
||
| 1886 | if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) ) |
||
| 1887 | continue; |
||
| 1888 | |||
| 1889 | if ( ! $ptype_obj->hierarchical ) { |
||
| 1890 | // Non-hierarchical post types can directly use 'name'. |
||
| 1891 | $q['name'] = $q[ $ptype_obj->query_var ]; |
||
| 1892 | } else { |
||
| 1893 | // Hierarchical post types will operate through 'pagename'. |
||
| 1894 | $q['pagename'] = $q[ $ptype_obj->query_var ]; |
||
| 1895 | $q['name'] = ''; |
||
| 1896 | } |
||
| 1897 | |||
| 1898 | // Only one request for a slug is possible, this is why name & pagename are overwritten above. |
||
| 1899 | break; |
||
| 1900 | } //end foreach |
||
| 1901 | unset($ptype_obj); |
||
| 1902 | } |
||
| 1903 | |||
| 1904 | if ( '' !== $q['title'] ) { |
||
| 1905 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); |
||
| 1906 | } |
||
| 1907 | |||
| 1908 | // Parameters related to 'post_name'. |
||
| 1909 | if ( '' != $q['name'] ) { |
||
| 1910 | $q['name'] = sanitize_title_for_query( $q['name'] ); |
||
| 1911 | $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; |
||
| 1912 | } elseif ( '' != $q['pagename'] ) { |
||
| 1913 | if ( isset($this->queried_object_id) ) { |
||
| 1914 | $reqpage = $this->queried_object_id; |
||
| 1915 | } else { |
||
| 1916 | if ( 'page' != $q['post_type'] ) { |
||
| 1917 | foreach ( (array)$q['post_type'] as $_post_type ) { |
||
| 1918 | $ptype_obj = get_post_type_object($_post_type); |
||
| 1919 | if ( !$ptype_obj || !$ptype_obj->hierarchical ) |
||
| 1920 | continue; |
||
| 1921 | |||
| 1922 | $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type); |
||
| 1923 | if ( $reqpage ) |
||
| 1924 | break; |
||
| 1925 | } |
||
| 1926 | unset($ptype_obj); |
||
| 1927 | } else { |
||
| 1928 | $reqpage = get_page_by_path($q['pagename']); |
||
| 1929 | } |
||
| 1930 | if ( !empty($reqpage) ) |
||
| 1931 | $reqpage = $reqpage->ID; |
||
| 1932 | else |
||
| 1933 | $reqpage = 0; |
||
| 1934 | } |
||
| 1935 | |||
| 1936 | $page_for_posts = get_option('page_for_posts'); |
||
| 1937 | if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) { |
||
| 1938 | $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); |
||
| 1939 | $q['name'] = $q['pagename']; |
||
| 1940 | $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; |
||
| 1941 | $reqpage_obj = get_post( $reqpage ); |
||
| 1942 | if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) { |
||
| 1943 | $this->is_attachment = true; |
||
| 1944 | $post_type = $q['post_type'] = 'attachment'; |
||
| 1945 | $this->is_page = true; |
||
| 1946 | $q['attachment_id'] = $reqpage; |
||
| 1947 | } |
||
| 1948 | } |
||
| 1949 | } elseif ( '' != $q['attachment'] ) { |
||
| 1950 | $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); |
||
| 1951 | $q['name'] = $q['attachment']; |
||
| 1952 | $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; |
||
| 1953 | } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { |
||
| 1954 | $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); |
||
| 1955 | $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; |
||
| 1956 | $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; |
||
| 1957 | } |
||
| 1958 | |||
| 1959 | // If an attachment is requested by number, let it supersede any post number. |
||
| 1960 | if ( $q['attachment_id'] ) |
||
| 1961 | $q['p'] = absint($q['attachment_id']); |
||
| 1962 | |||
| 1963 | // If a post number is specified, load that post |
||
| 1964 | View Code Duplication | if ( $q['p'] ) { |
|
| 1965 | $where .= " AND {$wpdb->posts}.ID = " . $q['p']; |
||
| 1966 | } elseif ( $q['post__in'] ) { |
||
| 1967 | $post__in = implode(',', array_map( 'absint', $q['post__in'] )); |
||
| 1968 | $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; |
||
| 1969 | } elseif ( $q['post__not_in'] ) { |
||
| 1970 | $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] )); |
||
| 1971 | $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; |
||
| 1972 | } |
||
| 1973 | |||
| 1974 | View Code Duplication | if ( is_numeric( $q['post_parent'] ) ) { |
|
| 1975 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); |
||
| 1976 | } elseif ( $q['post_parent__in'] ) { |
||
| 1977 | $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); |
||
| 1978 | $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; |
||
| 1979 | } elseif ( $q['post_parent__not_in'] ) { |
||
| 1980 | $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); |
||
| 1981 | $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; |
||
| 1982 | } |
||
| 1983 | |||
| 1984 | if ( $q['page_id'] ) { |
||
| 1985 | if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) { |
||
| 1986 | $q['p'] = $q['page_id']; |
||
| 1987 | $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; |
||
| 1988 | } |
||
| 1989 | } |
||
| 1990 | |||
| 1991 | // If a search pattern is specified, load the posts that match. |
||
| 1992 | if ( strlen( $q['s'] ) ) { |
||
| 1993 | $search = $this->parse_search( $q ); |
||
| 1994 | } |
||
| 1995 | |||
| 1996 | if ( ! $q['suppress_filters'] ) { |
||
| 1997 | /** |
||
| 1998 | * Filters the search SQL that is used in the WHERE clause of WP_Query. |
||
| 1999 | * |
||
| 2000 | * @since 3.0.0 |
||
| 2001 | * |
||
| 2002 | * @param string $search Search SQL for WHERE clause. |
||
| 2003 | * @param WP_Query $this The current WP_Query object. |
||
| 2004 | */ |
||
| 2005 | $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) ); |
||
| 2006 | } |
||
| 2007 | |||
| 2008 | // Taxonomies |
||
| 2009 | if ( !$this->is_singular ) { |
||
| 2010 | $this->parse_tax_query( $q ); |
||
| 2011 | |||
| 2012 | $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' ); |
||
| 2013 | |||
| 2014 | $join .= $clauses['join']; |
||
| 2015 | $where .= $clauses['where']; |
||
| 2016 | } |
||
| 2017 | |||
| 2018 | if ( $this->is_tax ) { |
||
| 2019 | if ( empty($post_type) ) { |
||
| 2020 | // Do a fully inclusive search for currently registered post types of queried taxonomies |
||
| 2021 | $post_type = array(); |
||
| 2022 | $taxonomies = array_keys( $this->tax_query->queried_terms ); |
||
| 2023 | foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) { |
||
| 2024 | $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt ); |
||
| 2025 | if ( array_intersect( $taxonomies, $object_taxonomies ) ) |
||
| 2026 | $post_type[] = $pt; |
||
| 2027 | } |
||
| 2028 | if ( ! $post_type ) |
||
| 2029 | $post_type = 'any'; |
||
| 2030 | elseif ( count( $post_type ) == 1 ) |
||
| 2031 | $post_type = $post_type[0]; |
||
| 2032 | |||
| 2033 | $post_status_join = true; |
||
| 2034 | } elseif ( in_array('attachment', (array) $post_type) ) { |
||
| 2035 | $post_status_join = true; |
||
| 2036 | } |
||
| 2037 | } |
||
| 2038 | |||
| 2039 | /* |
||
| 2040 | * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and |
||
| 2041 | * 'category_name' vars are set for backward compatibility. |
||
| 2042 | */ |
||
| 2043 | if ( ! empty( $this->tax_query->queried_terms ) ) { |
||
| 2044 | |||
| 2045 | /* |
||
| 2046 | * Set 'taxonomy', 'term', and 'term_id' to the |
||
| 2047 | * first taxonomy other than 'post_tag' or 'category'. |
||
| 2048 | */ |
||
| 2049 | if ( ! isset( $q['taxonomy'] ) ) { |
||
| 2050 | foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { |
||
| 2051 | if ( empty( $queried_items['terms'][0] ) ) { |
||
| 2052 | continue; |
||
| 2053 | } |
||
| 2054 | |||
| 2055 | if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) { |
||
| 2056 | $q['taxonomy'] = $queried_taxonomy; |
||
| 2057 | |||
| 2058 | if ( 'slug' === $queried_items['field'] ) { |
||
| 2059 | $q['term'] = $queried_items['terms'][0]; |
||
| 2060 | } else { |
||
| 2061 | $q['term_id'] = $queried_items['terms'][0]; |
||
| 2062 | } |
||
| 2063 | |||
| 2064 | // Take the first one we find. |
||
| 2065 | break; |
||
| 2066 | } |
||
| 2067 | } |
||
| 2068 | } |
||
| 2069 | |||
| 2070 | // 'cat', 'category_name', 'tag_id' |
||
| 2071 | foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) { |
||
| 2072 | if ( empty( $queried_items['terms'][0] ) ) { |
||
| 2073 | continue; |
||
| 2074 | } |
||
| 2075 | |||
| 2076 | View Code Duplication | if ( 'category' === $queried_taxonomy ) { |
|
| 2077 | $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' ); |
||
| 2078 | if ( $the_cat ) { |
||
| 2079 | $this->set( 'cat', $the_cat->term_id ); |
||
| 2080 | $this->set( 'category_name', $the_cat->slug ); |
||
| 2081 | } |
||
| 2082 | unset( $the_cat ); |
||
| 2083 | } |
||
| 2084 | |||
| 2085 | View Code Duplication | if ( 'post_tag' === $queried_taxonomy ) { |
|
| 2086 | $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' ); |
||
| 2087 | if ( $the_tag ) { |
||
| 2088 | $this->set( 'tag_id', $the_tag->term_id ); |
||
| 2089 | } |
||
| 2090 | unset( $the_tag ); |
||
| 2091 | } |
||
| 2092 | } |
||
| 2093 | } |
||
| 2094 | |||
| 2095 | if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { |
||
| 2096 | $groupby = "{$wpdb->posts}.ID"; |
||
| 2097 | } |
||
| 2098 | |||
| 2099 | // Author/user stuff |
||
| 2100 | |||
| 2101 | if ( ! empty( $q['author'] ) && $q['author'] != '0' ) { |
||
| 2102 | $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); |
||
| 2103 | $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); |
||
| 2104 | foreach ( $authors as $author ) { |
||
| 2105 | $key = $author > 0 ? 'author__in' : 'author__not_in'; |
||
| 2106 | $q[$key][] = abs( $author ); |
||
| 2107 | } |
||
| 2108 | $q['author'] = implode( ',', $authors ); |
||
| 2109 | } |
||
| 2110 | |||
| 2111 | if ( ! empty( $q['author__not_in'] ) ) { |
||
| 2112 | $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); |
||
| 2113 | $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; |
||
| 2114 | } elseif ( ! empty( $q['author__in'] ) ) { |
||
| 2115 | $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); |
||
| 2116 | $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; |
||
| 2117 | } |
||
| 2118 | |||
| 2119 | // Author stuff for nice URLs |
||
| 2120 | |||
| 2121 | if ( '' != $q['author_name'] ) { |
||
| 2122 | if ( strpos($q['author_name'], '/') !== false ) { |
||
| 2123 | $q['author_name'] = explode('/', $q['author_name']); |
||
| 2124 | if ( $q['author_name'][ count($q['author_name'])-1 ] ) { |
||
| 2125 | $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash |
||
| 2126 | } else { |
||
| 2127 | $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash |
||
| 2128 | } |
||
| 2129 | } |
||
| 2130 | $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); |
||
| 2131 | $q['author'] = get_user_by('slug', $q['author_name']); |
||
| 2132 | if ( $q['author'] ) |
||
| 2133 | $q['author'] = $q['author']->ID; |
||
| 2134 | $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')'; |
||
| 2135 | } |
||
| 2136 | |||
| 2137 | // MIME-Type stuff for attachment browsing |
||
| 2138 | |||
| 2139 | if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) { |
||
| 2140 | $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts ); |
||
| 2141 | } |
||
| 2142 | $where .= $search . $whichauthor . $whichmimetype; |
||
| 2143 | |||
| 2144 | if ( ! empty( $this->meta_query->queries ) ) { |
||
| 2145 | $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this ); |
||
| 2146 | $join .= $clauses['join']; |
||
| 2147 | $where .= $clauses['where']; |
||
| 2148 | } |
||
| 2149 | |||
| 2150 | $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] ); |
||
| 2151 | if ( ! isset( $q['order'] ) ) { |
||
| 2152 | $q['order'] = $rand ? '' : 'DESC'; |
||
| 2153 | } else { |
||
| 2154 | $q['order'] = $rand ? '' : $this->parse_order( $q['order'] ); |
||
| 2155 | } |
||
| 2156 | |||
| 2157 | // Order by. |
||
| 2158 | if ( empty( $q['orderby'] ) ) { |
||
| 2159 | /* |
||
| 2160 | * Boolean false or empty array blanks out ORDER BY, |
||
| 2161 | * while leaving the value unset or otherwise empty sets the default. |
||
| 2162 | */ |
||
| 2163 | if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) { |
||
| 2164 | $orderby = ''; |
||
| 2165 | } else { |
||
| 2166 | $orderby = "{$wpdb->posts}.post_date " . $q['order']; |
||
| 2167 | } |
||
| 2168 | } elseif ( 'none' == $q['orderby'] ) { |
||
| 2169 | $orderby = ''; |
||
| 2170 | } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) { |
||
| 2171 | $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )"; |
||
| 2172 | } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) { |
||
| 2173 | $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )"; |
||
| 2174 | } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) { |
||
| 2175 | $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )"; |
||
| 2176 | } else { |
||
| 2177 | $orderby_array = array(); |
||
| 2178 | if ( is_array( $q['orderby'] ) ) { |
||
| 2179 | foreach ( $q['orderby'] as $_orderby => $order ) { |
||
| 2180 | $orderby = addslashes_gpc( urldecode( $_orderby ) ); |
||
| 2181 | $parsed = $this->parse_orderby( $orderby ); |
||
| 2182 | |||
| 2183 | if ( ! $parsed ) { |
||
| 2184 | continue; |
||
| 2185 | } |
||
| 2186 | |||
| 2187 | $orderby_array[] = $parsed . ' ' . $this->parse_order( $order ); |
||
| 2188 | } |
||
| 2189 | $orderby = implode( ', ', $orderby_array ); |
||
| 2190 | |||
| 2191 | } else { |
||
| 2192 | $q['orderby'] = urldecode( $q['orderby'] ); |
||
| 2193 | $q['orderby'] = addslashes_gpc( $q['orderby'] ); |
||
| 2194 | |||
| 2195 | foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) { |
||
| 2196 | $parsed = $this->parse_orderby( $orderby ); |
||
| 2197 | // Only allow certain values for safety. |
||
| 2198 | if ( ! $parsed ) { |
||
| 2199 | continue; |
||
| 2200 | } |
||
| 2201 | |||
| 2202 | $orderby_array[] = $parsed; |
||
| 2203 | } |
||
| 2204 | $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array ); |
||
| 2205 | |||
| 2206 | if ( empty( $orderby ) ) { |
||
| 2207 | $orderby = "{$wpdb->posts}.post_date " . $q['order']; |
||
| 2208 | } elseif ( ! empty( $q['order'] ) ) { |
||
| 2209 | $orderby .= " {$q['order']}"; |
||
| 2210 | } |
||
| 2211 | } |
||
| 2212 | } |
||
| 2213 | |||
| 2214 | // Order search results by relevance only when another "orderby" is not specified in the query. |
||
| 2215 | if ( ! empty( $q['s'] ) ) { |
||
| 2216 | $search_orderby = ''; |
||
| 2217 | if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) ) |
||
| 2218 | $search_orderby = $this->parse_search_order( $q ); |
||
| 2219 | |||
| 2220 | if ( ! $q['suppress_filters'] ) { |
||
| 2221 | /** |
||
| 2222 | * Filters the ORDER BY used when ordering search results. |
||
| 2223 | * |
||
| 2224 | * @since 3.7.0 |
||
| 2225 | * |
||
| 2226 | * @param string $search_orderby The ORDER BY clause. |
||
| 2227 | * @param WP_Query $this The current WP_Query instance. |
||
| 2228 | */ |
||
| 2229 | $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); |
||
| 2230 | } |
||
| 2231 | |||
| 2232 | if ( $search_orderby ) |
||
| 2233 | $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; |
||
| 2234 | } |
||
| 2235 | |||
| 2236 | if ( is_array( $post_type ) && count( $post_type ) > 1 ) { |
||
| 2237 | $post_type_cap = 'multiple_post_type'; |
||
| 2238 | } else { |
||
| 2239 | if ( is_array( $post_type ) ) |
||
| 2240 | $post_type = reset( $post_type ); |
||
| 2241 | $post_type_object = get_post_type_object( $post_type ); |
||
| 2242 | if ( empty( $post_type_object ) ) |
||
| 2243 | $post_type_cap = $post_type; |
||
| 2244 | } |
||
| 2245 | |||
| 2246 | if ( isset( $q['post_password'] ) ) { |
||
| 2247 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] ); |
||
| 2248 | if ( empty( $q['perm'] ) ) { |
||
| 2249 | $q['perm'] = 'readable'; |
||
| 2250 | } |
||
| 2251 | } elseif ( isset( $q['has_password'] ) ) { |
||
| 2252 | $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' ); |
||
| 2253 | } |
||
| 2254 | |||
| 2255 | View Code Duplication | if ( ! empty( $q['comment_status'] ) ) { |
|
| 2256 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); |
||
| 2257 | } |
||
| 2258 | |||
| 2259 | View Code Duplication | if ( ! empty( $q['ping_status'] ) ) { |
|
| 2260 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); |
||
| 2261 | } |
||
| 2262 | |||
| 2263 | if ( 'any' == $post_type ) { |
||
| 2264 | $in_search_post_types = get_post_types( array('exclude_from_search' => false) ); |
||
| 2265 | if ( empty( $in_search_post_types ) ) { |
||
| 2266 | $where .= ' AND 1=0 '; |
||
| 2267 | } else { |
||
| 2268 | $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; |
||
| 2269 | } |
||
| 2270 | } elseif ( !empty( $post_type ) && is_array( $post_type ) ) { |
||
| 2271 | $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", esc_sql( $post_type ) ) . "')"; |
||
| 2272 | } elseif ( ! empty( $post_type ) ) { |
||
| 2273 | $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); |
||
| 2274 | $post_type_object = get_post_type_object ( $post_type ); |
||
| 2275 | } elseif ( $this->is_attachment ) { |
||
| 2276 | $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; |
||
| 2277 | $post_type_object = get_post_type_object ( 'attachment' ); |
||
| 2278 | } elseif ( $this->is_page ) { |
||
| 2279 | $where .= " AND {$wpdb->posts}.post_type = 'page'"; |
||
| 2280 | $post_type_object = get_post_type_object ( 'page' ); |
||
| 2281 | } else { |
||
| 2282 | $where .= " AND {$wpdb->posts}.post_type = 'post'"; |
||
| 2283 | $post_type_object = get_post_type_object ( 'post' ); |
||
| 2284 | } |
||
| 2285 | |||
| 2286 | $edit_cap = 'edit_post'; |
||
| 2287 | $read_cap = 'read_post'; |
||
| 2288 | |||
| 2289 | if ( ! empty( $post_type_object ) ) { |
||
| 2290 | $edit_others_cap = $post_type_object->cap->edit_others_posts; |
||
| 2291 | $read_private_cap = $post_type_object->cap->read_private_posts; |
||
| 2292 | } else { |
||
| 2293 | $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; |
||
| 2294 | $read_private_cap = 'read_private_' . $post_type_cap . 's'; |
||
| 2295 | } |
||
| 2296 | |||
| 2297 | $user_id = get_current_user_id(); |
||
| 2298 | |||
| 2299 | $q_status = array(); |
||
| 2300 | if ( ! empty( $q['post_status'] ) ) { |
||
| 2301 | $statuswheres = array(); |
||
| 2302 | $q_status = $q['post_status']; |
||
| 2303 | if ( ! is_array( $q_status ) ) |
||
| 2304 | $q_status = explode(',', $q_status); |
||
| 2305 | $r_status = array(); |
||
| 2306 | $p_status = array(); |
||
| 2307 | $e_status = array(); |
||
| 2308 | if ( in_array( 'any', $q_status ) ) { |
||
| 2309 | foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { |
||
| 2310 | if ( ! in_array( $status, $q_status ) ) { |
||
| 2311 | $e_status[] = "{$wpdb->posts}.post_status <> '$status'"; |
||
| 2312 | } |
||
| 2313 | } |
||
| 2314 | } else { |
||
| 2315 | foreach ( get_post_stati() as $status ) { |
||
| 2316 | if ( in_array( $status, $q_status ) ) { |
||
| 2317 | if ( 'private' == $status ) { |
||
| 2318 | $p_status[] = "{$wpdb->posts}.post_status = '$status'"; |
||
| 2319 | } else { |
||
| 2320 | $r_status[] = "{$wpdb->posts}.post_status = '$status'"; |
||
| 2321 | } |
||
| 2322 | } |
||
| 2323 | } |
||
| 2324 | } |
||
| 2325 | |||
| 2326 | if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) { |
||
| 2327 | $r_status = array_merge($r_status, $p_status); |
||
| 2328 | unset($p_status); |
||
| 2329 | } |
||
| 2330 | |||
| 2331 | if ( !empty($e_status) ) { |
||
| 2332 | $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")"; |
||
| 2333 | } |
||
| 2334 | View Code Duplication | if ( !empty($r_status) ) { |
|
| 2335 | if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) { |
||
| 2336 | $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))"; |
||
| 2337 | } else { |
||
| 2338 | $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; |
||
| 2339 | } |
||
| 2340 | } |
||
| 2341 | View Code Duplication | if ( !empty($p_status) ) { |
|
| 2342 | if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) { |
||
| 2343 | $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))"; |
||
| 2344 | } else { |
||
| 2345 | $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; |
||
| 2346 | } |
||
| 2347 | } |
||
| 2348 | if ( $post_status_join ) { |
||
| 2349 | $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; |
||
| 2350 | foreach ( $statuswheres as $index => $statuswhere ) { |
||
| 2351 | $statuswheres[$index] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . "))"; |
||
| 2352 | } |
||
| 2353 | } |
||
| 2354 | $where_status = implode( ' OR ', $statuswheres ); |
||
| 2355 | if ( ! empty( $where_status ) ) { |
||
| 2356 | $where .= " AND ($where_status)"; |
||
| 2357 | } |
||
| 2358 | } elseif ( !$this->is_singular ) { |
||
| 2359 | $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; |
||
| 2360 | |||
| 2361 | // Add public states. |
||
| 2362 | $public_states = get_post_stati( array('public' => true) ); |
||
| 2363 | foreach ( (array) $public_states as $state ) { |
||
| 2364 | if ( 'publish' == $state ) // Publish is hard-coded above. |
||
| 2365 | continue; |
||
| 2366 | $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
||
| 2367 | } |
||
| 2368 | |||
| 2369 | if ( $this->is_admin ) { |
||
| 2370 | // Add protected states that should show in the admin all list. |
||
| 2371 | $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) ); |
||
| 2372 | foreach ( (array) $admin_all_states as $state ) { |
||
| 2373 | $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
||
| 2374 | } |
||
| 2375 | } |
||
| 2376 | |||
| 2377 | if ( is_user_logged_in() ) { |
||
| 2378 | // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. |
||
| 2379 | $private_states = get_post_stati( array('private' => true) ); |
||
| 2380 | foreach ( (array) $private_states as $state ) { |
||
| 2381 | $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'"; |
||
| 2382 | } |
||
| 2383 | } |
||
| 2384 | |||
| 2385 | $where .= ')'; |
||
| 2386 | } |
||
| 2387 | |||
| 2388 | /* |
||
| 2389 | * Apply filters on where and join prior to paging so that any |
||
| 2390 | * manipulations to them are reflected in the paging by day queries. |
||
| 2391 | */ |
||
| 2392 | if ( !$q['suppress_filters'] ) { |
||
| 2393 | /** |
||
| 2394 | * Filters the WHERE clause of the query. |
||
| 2395 | * |
||
| 2396 | * @since 1.5.0 |
||
| 2397 | * |
||
| 2398 | * @param string $where The WHERE clause of the query. |
||
| 2399 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2400 | */ |
||
| 2401 | $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) ); |
||
| 2402 | |||
| 2403 | /** |
||
| 2404 | * Filters the JOIN clause of the query. |
||
| 2405 | * |
||
| 2406 | * @since 1.5.0 |
||
| 2407 | * |
||
| 2408 | * @param string $join The JOIN clause of the query. |
||
| 2409 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2410 | */ |
||
| 2411 | $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) ); |
||
| 2412 | } |
||
| 2413 | |||
| 2414 | // Paging |
||
| 2415 | if ( empty($q['nopaging']) && !$this->is_singular ) { |
||
| 2416 | $page = absint($q['paged']); |
||
| 2417 | if ( !$page ) |
||
| 2418 | $page = 1; |
||
| 2419 | |||
| 2420 | // If 'offset' is provided, it takes precedence over 'paged'. |
||
| 2421 | if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { |
||
| 2422 | $q['offset'] = absint( $q['offset'] ); |
||
| 2423 | $pgstrt = $q['offset'] . ', '; |
||
| 2424 | } else { |
||
| 2425 | $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; |
||
| 2426 | } |
||
| 2427 | $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; |
||
| 2428 | } |
||
| 2429 | |||
| 2430 | // Comments feeds |
||
| 2431 | if ( $this->is_comment_feed && ! $this->is_singular ) { |
||
| 2432 | if ( $this->is_archive || $this->is_search ) { |
||
| 2433 | $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join "; |
||
| 2434 | $cwhere = "WHERE comment_approved = '1' $where"; |
||
| 2435 | $cgroupby = "{$wpdb->comments}.comment_id"; |
||
| 2436 | } else { // Other non singular e.g. front |
||
| 2437 | $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )"; |
||
| 2438 | $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'"; |
||
| 2439 | $cgroupby = ''; |
||
| 2440 | } |
||
| 2441 | |||
| 2442 | if ( !$q['suppress_filters'] ) { |
||
| 2443 | /** |
||
| 2444 | * Filters the JOIN clause of the comments feed query before sending. |
||
| 2445 | * |
||
| 2446 | * @since 2.2.0 |
||
| 2447 | * |
||
| 2448 | * @param string $cjoin The JOIN clause of the query. |
||
| 2449 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2450 | */ |
||
| 2451 | $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) ); |
||
| 2452 | |||
| 2453 | /** |
||
| 2454 | * Filters the WHERE clause of the comments feed query before sending. |
||
| 2455 | * |
||
| 2456 | * @since 2.2.0 |
||
| 2457 | * |
||
| 2458 | * @param string $cwhere The WHERE clause of the query. |
||
| 2459 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2460 | */ |
||
| 2461 | $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) ); |
||
| 2462 | |||
| 2463 | /** |
||
| 2464 | * Filters the GROUP BY clause of the comments feed query before sending. |
||
| 2465 | * |
||
| 2466 | * @since 2.2.0 |
||
| 2467 | * |
||
| 2468 | * @param string $cgroupby The GROUP BY clause of the query. |
||
| 2469 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2470 | */ |
||
| 2471 | $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) ); |
||
| 2472 | |||
| 2473 | /** |
||
| 2474 | * Filters the ORDER BY clause of the comments feed query before sending. |
||
| 2475 | * |
||
| 2476 | * @since 2.8.0 |
||
| 2477 | * |
||
| 2478 | * @param string $corderby The ORDER BY clause of the query. |
||
| 2479 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2480 | */ |
||
| 2481 | $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); |
||
| 2482 | |||
| 2483 | /** |
||
| 2484 | * Filters the LIMIT clause of the comments feed query before sending. |
||
| 2485 | * |
||
| 2486 | * @since 2.8.0 |
||
| 2487 | * |
||
| 2488 | * @param string $climits The JOIN clause of the query. |
||
| 2489 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2490 | */ |
||
| 2491 | $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); |
||
| 2492 | } |
||
| 2493 | $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; |
||
| 2494 | $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
||
| 2495 | |||
| 2496 | $comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"); |
||
| 2497 | // Convert to WP_Comment |
||
| 2498 | $this->comments = array_map( 'get_comment', $comments ); |
||
| 2499 | $this->comment_count = count($this->comments); |
||
| 2500 | |||
| 2501 | $post_ids = array(); |
||
| 2502 | |||
| 2503 | foreach ( $this->comments as $comment ) |
||
| 2504 | $post_ids[] = (int) $comment->comment_post_ID; |
||
| 2505 | |||
| 2506 | $post_ids = join(',', $post_ids); |
||
| 2507 | $join = ''; |
||
| 2508 | if ( $post_ids ) { |
||
| 2509 | $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; |
||
| 2510 | } else { |
||
| 2511 | $where = "AND 0"; |
||
| 2512 | } |
||
| 2513 | } |
||
| 2514 | |||
| 2515 | $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); |
||
| 2516 | |||
| 2517 | /* |
||
| 2518 | * Apply post-paging filters on where and join. Only plugins that |
||
| 2519 | * manipulate paging queries should use these hooks. |
||
| 2520 | */ |
||
| 2521 | View Code Duplication | if ( !$q['suppress_filters'] ) { |
|
| 2522 | /** |
||
| 2523 | * Filters the WHERE clause of the query. |
||
| 2524 | * |
||
| 2525 | * Specifically for manipulating paging queries. |
||
| 2526 | * |
||
| 2527 | * @since 1.5.0 |
||
| 2528 | * |
||
| 2529 | * @param string $where The WHERE clause of the query. |
||
| 2530 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2531 | */ |
||
| 2532 | $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); |
||
| 2533 | |||
| 2534 | /** |
||
| 2535 | * Filters the GROUP BY clause of the query. |
||
| 2536 | * |
||
| 2537 | * @since 2.0.0 |
||
| 2538 | * |
||
| 2539 | * @param string $groupby The GROUP BY clause of the query. |
||
| 2540 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2541 | */ |
||
| 2542 | $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); |
||
| 2543 | |||
| 2544 | /** |
||
| 2545 | * Filters the JOIN clause of the query. |
||
| 2546 | * |
||
| 2547 | * Specifically for manipulating paging queries. |
||
| 2548 | * |
||
| 2549 | * @since 1.5.0 |
||
| 2550 | * |
||
| 2551 | * @param string $join The JOIN clause of the query. |
||
| 2552 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2553 | */ |
||
| 2554 | $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); |
||
| 2555 | |||
| 2556 | /** |
||
| 2557 | * Filters the ORDER BY clause of the query. |
||
| 2558 | * |
||
| 2559 | * @since 1.5.1 |
||
| 2560 | * |
||
| 2561 | * @param string $orderby The ORDER BY clause of the query. |
||
| 2562 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2563 | */ |
||
| 2564 | $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); |
||
| 2565 | |||
| 2566 | /** |
||
| 2567 | * Filters the DISTINCT clause of the query. |
||
| 2568 | * |
||
| 2569 | * @since 2.1.0 |
||
| 2570 | * |
||
| 2571 | * @param string $distinct The DISTINCT clause of the query. |
||
| 2572 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2573 | */ |
||
| 2574 | $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); |
||
| 2575 | |||
| 2576 | /** |
||
| 2577 | * Filters the LIMIT clause of the query. |
||
| 2578 | * |
||
| 2579 | * @since 2.1.0 |
||
| 2580 | * |
||
| 2581 | * @param string $limits The LIMIT clause of the query. |
||
| 2582 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2583 | */ |
||
| 2584 | $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); |
||
| 2585 | |||
| 2586 | /** |
||
| 2587 | * Filters the SELECT clause of the query. |
||
| 2588 | * |
||
| 2589 | * @since 2.1.0 |
||
| 2590 | * |
||
| 2591 | * @param string $fields The SELECT clause of the query. |
||
| 2592 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2593 | */ |
||
| 2594 | $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); |
||
| 2595 | |||
| 2596 | /** |
||
| 2597 | * Filters all query clauses at once, for convenience. |
||
| 2598 | * |
||
| 2599 | * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, |
||
| 2600 | * fields (SELECT), and LIMITS clauses. |
||
| 2601 | * |
||
| 2602 | * @since 3.1.0 |
||
| 2603 | * |
||
| 2604 | * @param array $clauses The list of clauses for the query. |
||
| 2605 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2606 | */ |
||
| 2607 | $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); |
||
| 2608 | |||
| 2609 | $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; |
||
| 2610 | $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; |
||
| 2611 | $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; |
||
| 2612 | $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; |
||
| 2613 | $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; |
||
| 2614 | $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; |
||
| 2615 | $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; |
||
| 2616 | } |
||
| 2617 | |||
| 2618 | /** |
||
| 2619 | * Fires to announce the query's current selection parameters. |
||
| 2620 | * |
||
| 2621 | * For use by caching plugins. |
||
| 2622 | * |
||
| 2623 | * @since 2.3.0 |
||
| 2624 | * |
||
| 2625 | * @param string $selection The assembled selection query. |
||
| 2626 | */ |
||
| 2627 | do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join ); |
||
| 2628 | |||
| 2629 | /* |
||
| 2630 | * Filters again for the benefit of caching plugins. |
||
| 2631 | * Regular plugins should use the hooks above. |
||
| 2632 | */ |
||
| 2633 | View Code Duplication | if ( !$q['suppress_filters'] ) { |
|
| 2634 | /** |
||
| 2635 | * Filters the WHERE clause of the query. |
||
| 2636 | * |
||
| 2637 | * For use by caching plugins. |
||
| 2638 | * |
||
| 2639 | * @since 2.5.0 |
||
| 2640 | * |
||
| 2641 | * @param string $where The WHERE clause of the query. |
||
| 2642 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2643 | */ |
||
| 2644 | $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); |
||
| 2645 | |||
| 2646 | /** |
||
| 2647 | * Filters the GROUP BY clause of the query. |
||
| 2648 | * |
||
| 2649 | * For use by caching plugins. |
||
| 2650 | * |
||
| 2651 | * @since 2.5.0 |
||
| 2652 | * |
||
| 2653 | * @param string $groupby The GROUP BY clause of the query. |
||
| 2654 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2655 | */ |
||
| 2656 | $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); |
||
| 2657 | |||
| 2658 | /** |
||
| 2659 | * Filters the JOIN clause of the query. |
||
| 2660 | * |
||
| 2661 | * For use by caching plugins. |
||
| 2662 | * |
||
| 2663 | * @since 2.5.0 |
||
| 2664 | * |
||
| 2665 | * @param string $join The JOIN clause of the query. |
||
| 2666 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2667 | */ |
||
| 2668 | $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); |
||
| 2669 | |||
| 2670 | /** |
||
| 2671 | * Filters the ORDER BY clause of the query. |
||
| 2672 | * |
||
| 2673 | * For use by caching plugins. |
||
| 2674 | * |
||
| 2675 | * @since 2.5.0 |
||
| 2676 | * |
||
| 2677 | * @param string $orderby The ORDER BY clause of the query. |
||
| 2678 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2679 | */ |
||
| 2680 | $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); |
||
| 2681 | |||
| 2682 | /** |
||
| 2683 | * Filters the DISTINCT clause of the query. |
||
| 2684 | * |
||
| 2685 | * For use by caching plugins. |
||
| 2686 | * |
||
| 2687 | * @since 2.5.0 |
||
| 2688 | * |
||
| 2689 | * @param string $distinct The DISTINCT clause of the query. |
||
| 2690 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2691 | */ |
||
| 2692 | $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) ); |
||
| 2693 | |||
| 2694 | /** |
||
| 2695 | * Filters the SELECT clause of the query. |
||
| 2696 | * |
||
| 2697 | * For use by caching plugins. |
||
| 2698 | * |
||
| 2699 | * @since 2.5.0 |
||
| 2700 | * |
||
| 2701 | * @param string $fields The SELECT clause of the query. |
||
| 2702 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2703 | */ |
||
| 2704 | $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) ); |
||
| 2705 | |||
| 2706 | /** |
||
| 2707 | * Filters the LIMIT clause of the query. |
||
| 2708 | * |
||
| 2709 | * For use by caching plugins. |
||
| 2710 | * |
||
| 2711 | * @since 2.5.0 |
||
| 2712 | * |
||
| 2713 | * @param string $limits The LIMIT clause of the query. |
||
| 2714 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2715 | */ |
||
| 2716 | $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); |
||
| 2717 | |||
| 2718 | /** |
||
| 2719 | * Filters all query clauses at once, for convenience. |
||
| 2720 | * |
||
| 2721 | * For use by caching plugins. |
||
| 2722 | * |
||
| 2723 | * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, |
||
| 2724 | * fields (SELECT), and LIMITS clauses. |
||
| 2725 | * |
||
| 2726 | * @since 3.1.0 |
||
| 2727 | * |
||
| 2728 | * @param array $pieces The pieces of the query. |
||
| 2729 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2730 | */ |
||
| 2731 | $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); |
||
| 2732 | |||
| 2733 | $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; |
||
| 2734 | $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; |
||
| 2735 | $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; |
||
| 2736 | $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; |
||
| 2737 | $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; |
||
| 2738 | $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; |
||
| 2739 | $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; |
||
| 2740 | } |
||
| 2741 | |||
| 2742 | if ( ! empty($groupby) ) |
||
| 2743 | $groupby = 'GROUP BY ' . $groupby; |
||
| 2744 | if ( !empty( $orderby ) ) |
||
| 2745 | $orderby = 'ORDER BY ' . $orderby; |
||
| 2746 | |||
| 2747 | $found_rows = ''; |
||
| 2748 | if ( !$q['no_found_rows'] && !empty($limits) ) |
||
| 2749 | $found_rows = 'SQL_CALC_FOUND_ROWS'; |
||
| 2750 | |||
| 2751 | $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; |
||
| 2752 | |||
| 2753 | if ( !$q['suppress_filters'] ) { |
||
| 2754 | /** |
||
| 2755 | * Filters the completed SQL query before sending. |
||
| 2756 | * |
||
| 2757 | * @since 2.0.0 |
||
| 2758 | * |
||
| 2759 | * @param string $request The complete SQL query. |
||
| 2760 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2761 | */ |
||
| 2762 | $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); |
||
| 2763 | } |
||
| 2764 | |||
| 2765 | /** |
||
| 2766 | * Filters the posts array before the query takes place. |
||
| 2767 | * |
||
| 2768 | * Return a non-null value to bypass WordPress's default post queries. |
||
| 2769 | * |
||
| 2770 | * Filtering functions that require pagination information are encouraged to set |
||
| 2771 | * the `found_posts` and `max_num_pages` properties of the WP_Query object, |
||
| 2772 | * passed to the filter by reference. If WP_Query does not perform a database |
||
| 2773 | * query, it will not have enough information to generate these values itself. |
||
| 2774 | * |
||
| 2775 | * @since 4.6.0 |
||
| 2776 | * |
||
| 2777 | * @param array|null $posts Return an array of post data to short-circuit WP's query, |
||
| 2778 | * or null to allow WP to run its normal queries. |
||
| 2779 | * @param WP_Query $this The WP_Query instance, passed by reference. |
||
| 2780 | */ |
||
| 2781 | $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) ); |
||
| 2782 | |||
| 2783 | if ( 'ids' == $q['fields'] ) { |
||
| 2784 | if ( null === $this->posts ) { |
||
| 2785 | $this->posts = $wpdb->get_col( $this->request ); |
||
| 2786 | } |
||
| 2787 | |||
| 2788 | $this->posts = array_map( 'intval', $this->posts ); |
||
| 2789 | $this->post_count = count( $this->posts ); |
||
| 2790 | $this->set_found_posts( $q, $limits ); |
||
| 2791 | |||
| 2792 | return $this->posts; |
||
| 2793 | } |
||
| 2794 | |||
| 2795 | if ( 'id=>parent' == $q['fields'] ) { |
||
| 2796 | if ( null === $this->posts ) { |
||
| 2797 | $this->posts = $wpdb->get_results( $this->request ); |
||
| 2798 | } |
||
| 2799 | |||
| 2800 | $this->post_count = count( $this->posts ); |
||
| 2801 | $this->set_found_posts( $q, $limits ); |
||
| 2802 | |||
| 2803 | $r = array(); |
||
| 2804 | foreach ( $this->posts as $key => $post ) { |
||
| 2805 | $this->posts[ $key ]->ID = (int) $post->ID; |
||
| 2806 | $this->posts[ $key ]->post_parent = (int) $post->post_parent; |
||
| 2807 | |||
| 2808 | $r[ (int) $post->ID ] = (int) $post->post_parent; |
||
| 2809 | } |
||
| 2810 | |||
| 2811 | return $r; |
||
| 2812 | } |
||
| 2813 | |||
| 2814 | if ( null === $this->posts ) { |
||
| 2815 | $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 ); |
||
| 2816 | |||
| 2817 | /** |
||
| 2818 | * Filters whether to split the query. |
||
| 2819 | * |
||
| 2820 | * Splitting the query will cause it to fetch just the IDs of the found posts |
||
| 2821 | * (and then individually fetch each post by ID), rather than fetching every |
||
| 2822 | * complete row at once. One massive result vs. many small results. |
||
| 2823 | * |
||
| 2824 | * @since 3.4.0 |
||
| 2825 | * |
||
| 2826 | * @param bool $split_the_query Whether or not to split the query. |
||
| 2827 | * @param WP_Query $this The WP_Query instance. |
||
| 2828 | */ |
||
| 2829 | $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); |
||
| 2830 | |||
| 2831 | if ( $split_the_query ) { |
||
| 2832 | // First get the IDs and then fill in the objects |
||
| 2833 | |||
| 2834 | $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; |
||
| 2835 | |||
| 2836 | /** |
||
| 2837 | * Filters the Post IDs SQL request before sending. |
||
| 2838 | * |
||
| 2839 | * @since 3.4.0 |
||
| 2840 | * |
||
| 2841 | * @param string $request The post ID request. |
||
| 2842 | * @param WP_Query $this The WP_Query instance. |
||
| 2843 | */ |
||
| 2844 | $this->request = apply_filters( 'posts_request_ids', $this->request, $this ); |
||
| 2845 | |||
| 2846 | $ids = $wpdb->get_col( $this->request ); |
||
| 2847 | |||
| 2848 | if ( $ids ) { |
||
| 2849 | $this->posts = $ids; |
||
| 2850 | $this->set_found_posts( $q, $limits ); |
||
| 2851 | _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] ); |
||
| 2852 | } else { |
||
| 2853 | $this->posts = array(); |
||
| 2854 | } |
||
| 2855 | } else { |
||
| 2856 | $this->posts = $wpdb->get_results( $this->request ); |
||
| 2857 | $this->set_found_posts( $q, $limits ); |
||
| 2858 | } |
||
| 2859 | } |
||
| 2860 | |||
| 2861 | // Convert to WP_Post objects. |
||
| 2862 | if ( $this->posts ) { |
||
| 2863 | $this->posts = array_map( 'get_post', $this->posts ); |
||
| 2864 | } |
||
| 2865 | |||
| 2866 | View Code Duplication | if ( ! $q['suppress_filters'] ) { |
|
| 2867 | /** |
||
| 2868 | * Filters the raw post results array, prior to status checks. |
||
| 2869 | * |
||
| 2870 | * @since 2.3.0 |
||
| 2871 | * |
||
| 2872 | * @param array $posts The post results array. |
||
| 2873 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2874 | */ |
||
| 2875 | $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) ); |
||
| 2876 | } |
||
| 2877 | |||
| 2878 | if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) { |
||
| 2879 | /** This filter is documented in wp-includes/query.php */ |
||
| 2880 | $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) ); |
||
| 2881 | |||
| 2882 | /** This filter is documented in wp-includes/query.php */ |
||
| 2883 | $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) ); |
||
| 2884 | |||
| 2885 | /** This filter is documented in wp-includes/query.php */ |
||
| 2886 | $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) ); |
||
| 2887 | $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; |
||
| 2888 | |||
| 2889 | /** This filter is documented in wp-includes/query.php */ |
||
| 2890 | $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); |
||
| 2891 | $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
||
| 2892 | |||
| 2893 | /** This filter is documented in wp-includes/query.php */ |
||
| 2894 | $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); |
||
| 2895 | |||
| 2896 | $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; |
||
| 2897 | $comments = $wpdb->get_results($comments_request); |
||
| 2898 | // Convert to WP_Comment |
||
| 2899 | $this->comments = array_map( 'get_comment', $comments ); |
||
| 2900 | $this->comment_count = count($this->comments); |
||
| 2901 | } |
||
| 2902 | |||
| 2903 | // Check post status to determine if post should be displayed. |
||
| 2904 | if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { |
||
| 2905 | $status = get_post_status($this->posts[0]); |
||
| 2906 | if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { |
||
| 2907 | $this->is_page = false; |
||
| 2908 | $this->is_single = true; |
||
| 2909 | $this->is_attachment = true; |
||
| 2910 | } |
||
| 2911 | $post_status_obj = get_post_status_object($status); |
||
| 2912 | |||
| 2913 | // If the post_status was specifically requested, let it pass through. |
||
| 2914 | if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) { |
||
| 2915 | |||
| 2916 | if ( ! is_user_logged_in() ) { |
||
| 2917 | // User must be logged in to view unpublished posts. |
||
| 2918 | $this->posts = array(); |
||
| 2919 | } else { |
||
| 2920 | if ( $post_status_obj->protected ) { |
||
| 2921 | // User must have edit permissions on the draft to preview. |
||
| 2922 | if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) { |
||
| 2923 | $this->posts = array(); |
||
| 2924 | } else { |
||
| 2925 | $this->is_preview = true; |
||
| 2926 | if ( 'future' != $status ) |
||
| 2927 | $this->posts[0]->post_date = current_time('mysql'); |
||
| 2928 | } |
||
| 2929 | } elseif ( $post_status_obj->private ) { |
||
| 2930 | if ( ! current_user_can($read_cap, $this->posts[0]->ID) ) |
||
| 2931 | $this->posts = array(); |
||
| 2932 | } else { |
||
| 2933 | $this->posts = array(); |
||
| 2934 | } |
||
| 2935 | } |
||
| 2936 | } |
||
| 2937 | |||
| 2938 | if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) { |
||
| 2939 | /** |
||
| 2940 | * Filters the single post for preview mode. |
||
| 2941 | * |
||
| 2942 | * @since 2.7.0 |
||
| 2943 | * |
||
| 2944 | * @param WP_Post $post_preview The Post object. |
||
| 2945 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 2946 | */ |
||
| 2947 | $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); |
||
| 2948 | } |
||
| 2949 | } |
||
| 2950 | |||
| 2951 | // Put sticky posts at the top of the posts array |
||
| 2952 | $sticky_posts = get_option('sticky_posts'); |
||
| 2953 | if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { |
||
| 2954 | $num_posts = count($this->posts); |
||
| 2955 | $sticky_offset = 0; |
||
| 2956 | // Loop over posts and relocate stickies to the front. |
||
| 2957 | for ( $i = 0; $i < $num_posts; $i++ ) { |
||
| 2958 | if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { |
||
| 2959 | $sticky_post = $this->posts[$i]; |
||
| 2960 | // Remove sticky from current position |
||
| 2961 | array_splice($this->posts, $i, 1); |
||
| 2962 | // Move to front, after other stickies |
||
| 2963 | array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); |
||
| 2964 | // Increment the sticky offset. The next sticky will be placed at this offset. |
||
| 2965 | $sticky_offset++; |
||
| 2966 | // Remove post from sticky posts array |
||
| 2967 | $offset = array_search($sticky_post->ID, $sticky_posts); |
||
| 2968 | unset( $sticky_posts[$offset] ); |
||
| 2969 | } |
||
| 2970 | } |
||
| 2971 | |||
| 2972 | // If any posts have been excluded specifically, Ignore those that are sticky. |
||
| 2973 | if ( !empty($sticky_posts) && !empty($q['post__not_in']) ) |
||
| 2974 | $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); |
||
| 2975 | |||
| 2976 | // Fetch sticky posts that weren't in the query results |
||
| 2977 | if ( !empty($sticky_posts) ) { |
||
| 2978 | $stickies = get_posts( array( |
||
| 2979 | 'post__in' => $sticky_posts, |
||
| 2980 | 'post_type' => $post_type, |
||
| 2981 | 'post_status' => 'publish', |
||
| 2982 | 'nopaging' => true |
||
| 2983 | ) ); |
||
| 2984 | |||
| 2985 | foreach ( $stickies as $sticky_post ) { |
||
| 2986 | array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); |
||
| 2987 | $sticky_offset++; |
||
| 2988 | } |
||
| 2989 | } |
||
| 2990 | } |
||
| 2991 | |||
| 2992 | // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up. |
||
| 2993 | if ( ! empty( $this->comments ) ) { |
||
| 2994 | wp_queue_comments_for_comment_meta_lazyload( $this->comments ); |
||
| 2995 | } |
||
| 2996 | |||
| 2997 | View Code Duplication | if ( ! $q['suppress_filters'] ) { |
|
| 2998 | /** |
||
| 2999 | * Filters the array of retrieved posts after they've been fetched and |
||
| 3000 | * internally processed. |
||
| 3001 | * |
||
| 3002 | * @since 1.5.0 |
||
| 3003 | * |
||
| 3004 | * @param array $posts The array of retrieved posts. |
||
| 3005 | * @param WP_Query &$this The WP_Query instance (passed by reference). |
||
| 3006 | */ |
||
| 3007 | $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) ); |
||
| 3008 | } |
||
| 3009 | |||
| 3010 | // Ensure that any posts added/modified via one of the filters above are |
||
| 3011 | // of the type WP_Post and are filtered. |
||
| 3012 | if ( $this->posts ) { |
||
| 3013 | $this->post_count = count( $this->posts ); |
||
| 3014 | |||
| 3015 | $this->posts = array_map( 'get_post', $this->posts ); |
||
| 3016 | |||
| 3017 | if ( $q['cache_results'] ) |
||
| 3018 | update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']); |
||
| 3019 | |||
| 3020 | $this->post = reset( $this->posts ); |
||
| 3021 | } else { |
||
| 3022 | $this->post_count = 0; |
||
| 3023 | $this->posts = array(); |
||
| 3024 | } |
||
| 3025 | |||
| 3026 | if ( $q['lazy_load_term_meta'] ) { |
||
| 3027 | wp_queue_posts_for_term_meta_lazyload( $this->posts ); |
||
| 3028 | } |
||
| 3029 | |||
| 3030 | return $this->posts; |
||
| 3031 | } |
||
| 3032 | |||
| 4119 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..