| | 1786 | * Test ordering by multiple meta keys. |
| | 1787 | * |
| | 1788 | * @ticket 38442 |
| | 1789 | */ |
| | 1790 | function test_orderby_clause_two_meta_keys() { |
| | 1791 | $posts = self::factory()->post->create_many( 4 ); |
| | 1792 | |
| | 1793 | add_post_meta( $posts[0], 'foo', 'jjj' ); |
| | 1794 | add_post_meta( $posts[1], 'foo', 'zzz' ); |
| | 1795 | add_post_meta( $posts[2], 'foo', 'aaa' ); |
| | 1796 | add_post_meta( $posts[3], 'foo', 'aaa' ); |
| | 1797 | |
| | 1798 | add_post_meta( $posts[3], 'bar', 150 ); |
| | 1799 | add_post_meta( $posts[2], 'bar', 100 ); |
| | 1800 | add_post_meta( $posts[1], 'bar', 200 ); |
| | 1801 | add_post_meta( $posts[0], 'bar', 300 ); |
| | 1802 | |
| | 1803 | $query_args = array( |
| | 1804 | 'fields' => 'ids', |
| | 1805 | 'post_type' => 'any', |
| | 1806 | 'meta_query' => array( |
| | 1807 | 'relation' => 'AND', |
| | 1808 | array( |
| | 1809 | 'key' => 'foo', |
| | 1810 | ), |
| | 1811 | array( |
| | 1812 | 'key' => 'bar', |
| | 1813 | 'type' => 'UNSIGNED', |
| | 1814 | ), |
| | 1815 | ), |
| | 1816 | 'orderby' => array( |
| | 1817 | 'foo' => 'ASC', |
| | 1818 | 'bar' => 'ASC', |
| | 1819 | ), |
| | 1820 | ); |
| | 1821 | |
| | 1822 | $query = new WP_Query( $query_args ); |
| | 1823 | |
| | 1824 | $this->assertEquals( array( $posts[2], $posts[3], $posts[0], $posts[1] ), $query->posts ); |
| | 1825 | $this->assertEquals( 2, substr_count( $query->request, 'ASC' ) ); |
| | 1826 | $this->assertEquals( 1, substr_count( $query->request, 'CAST(' ) ); |
| | 1827 | |
| | 1828 | $query_args['orderby'] = array( |
| | 1829 | 'foo' => 'ASC', |
| | 1830 | 'bar' => 'DESC', |
| | 1831 | ); |
| | 1832 | $query = new WP_Query( $query_args ); |
| | 1833 | |
| | 1834 | $this->assertEquals( array( $posts[3], $posts[2], $posts[0], $posts[1] ), $query->posts ); |
| | 1835 | $this->assertEquals( 1, substr_count( $query->request, 'ASC' ) ); |
| | 1836 | $this->assertEquals( 1, substr_count( $query->request, 'DESC' ) ); |
| | 1837 | |
| | 1838 | $query_args['orderby'] = array( |
| | 1839 | 'foo' => 'DESC', |
| | 1840 | 'bar' => 'DESC', |
| | 1841 | ); |
| | 1842 | $query = new WP_Query( $query_args ); |
| | 1843 | |
| | 1844 | $this->assertEquals( array( $posts[1], $posts[0], $posts[3], $posts[2] ), $query->posts ); |
| | 1845 | $this->assertEquals( 2, substr_count( $query->request, 'DESC' ) ); |
| | 1846 | } |
| | 1847 | |
| | 1848 | /** |