get_adjacent_post()

You are here:

get_adjacent_post( bool $in_same_term = falsearray|string $excluded_terms = bool $previous = truestring $taxonomy = ‘category’ )

Retrieves the adjacent post.

Description 

Can either be next or previous post.

Parameters 

$in_same_term

(bool) (Optional) Whether post should be in a same taxonomy term.

Default value: false

$excluded_terms

(array|string) (Optional) Array or comma-separated list of excluded term IDs.

Default value: ”

$previous

(bool) (Optional) Whether to retrieve previous post. Default true

Default value: true

$taxonomy

(string) (Optional) Taxonomy, if $in_same_term is true.

Default value: ‘category’


Top

Return 

(null|string|WP_Post) Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.


Top

Source 

File: wp-includes/link-template.php

1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    global $wpdb;
 
    $post = get_post();
    if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {
        return null;
    }
 
    $current_post_date = $post->post_date;
 
    $join     = '';
    $where    = '';
    $adjacent = $previous ? 'previous' : 'next';
 
    if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
        // Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".
        if ( false !== strpos( $excluded_terms, ' and ' ) ) {
            _deprecated_argument(
                __FUNCTION__,
                '3.3.0',
                sprintf(
                    /* translators: %s: The word 'and'. */
                    __( 'Use commas instead of %s to separate excluded terms.' ),
                    "'and'"
                )
            );
            $excluded_terms = explode( ' and ', $excluded_terms );
        } else {
            $excluded_terms = explode( ',', $excluded_terms );
        }
 
        $excluded_terms = array_map( 'intval', $excluded_terms );
    }
 
    /**
     * Filters the IDs of terms excluded from adjacent post queries.
     *
     * The dynamic portion of the hook name, `$adjacent`, refers to the type
     * of adjacency, 'next' or 'previous'.
     *
     * @since 4.4.0
     *
     * @param array $excluded_terms Array of excluded term IDs.
     */
    $excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
 
    if ( $in_same_term || ! empty( $excluded_terms ) ) {
        if ( $in_same_term ) {
            $join  .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
            $where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );
 
            if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
                return '';
            }
            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
 
            // Remove any exclusions from the term array to include.
            $term_array = array_diff( $term_array, (array) $excluded_terms );
            $term_array = array_map( 'intval', $term_array );
 
            if ( ! $term_array || is_wp_error( $term_array ) ) {
                return '';
            }
 
            $where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
        }
 
        if ( ! empty( $excluded_terms ) ) {
            $where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
        }
    }
 
    // 'post_status' clause depends on the current user.
    if ( is_user_logged_in() ) {
        $user_id = get_current_user_id();
 
        $post_type_object = get_post_type_object( $post->post_type );
        if ( empty( $post_type_object ) ) {
            $post_type_cap    = $post->post_type;
            $read_private_cap = 'read_private_' . $post_type_cap . 's';
        } else {
            $read_private_cap = $post_type_object->cap->read_private_posts;
        }
 
        /*
         * Results should include private posts belonging to the current user, or private posts where the
         * current user has the 'read_private_posts' cap.
         */
        $private_states = get_post_stati( array( 'private' => true ) );
        $where         .= " AND ( p.post_status = 'publish'";
        foreach ( (array) $private_states as $state ) {
            if ( current_user_can( $read_private_cap ) ) {
                $where .= $wpdb->prepare( ' OR p.post_status = %s', $state );
            } else {
                $where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state );
            }
        }
        $where .= ' )';
    } else {
        $where .= " AND p.post_status = 'publish'";
    }
 
    $op    = $previous ? '<' : '>';
    $order = $previous ? 'DESC' : 'ASC';
 
    /**
     * Filters the JOIN clause in the SQL for an adjacent post query.
     *
     * The dynamic portion of the hook name, `$adjacent`, refers to the type
     * of adjacency, 'next' or 'previous'.
     *
     * @since 2.5.0
     * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
     *
     * @param string  $join           The JOIN clause in the SQL.
     * @param bool    $in_same_term   Whether post should be in a same taxonomy term.
     * @param array   $excluded_terms Array of excluded term IDs.
     * @param string  $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
     * @param WP_Post $post           WP_Post object.
     */
    $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
 
    /**
     * Filters the WHERE clause in the SQL for an adjacent post query.
     *
     * The dynamic portion of the hook name, `$adjacent`, refers to the type
     * of adjacency, 'next' or 'previous'.
     *
     * @since 2.5.0
     * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
     *
     * @param string  $where          The `WHERE` clause in the SQL.
     * @param bool    $in_same_term   Whether post should be in a same taxonomy term.
     * @param array   $excluded_terms Array of excluded term IDs.
     * @param string  $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
     * @param WP_Post $post           WP_Post object.
     */
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
 
    /**
     * Filters the ORDER BY clause in the SQL for an adjacent post query.
     *
     * The dynamic portion of the hook name, `$adjacent`, refers to the type
     * of adjacency, 'next' or 'previous'.
     *
     * @since 2.5.0
     * @since 4.4.0 Added the `$post` parameter.
     * @since 4.9.0 Added the `$order` parameter.
     *
     * @param string $order_by The `ORDER BY` clause in the SQL.
     * @param WP_Post $post    WP_Post object.
     * @param string  $order   Sort order. 'DESC' for previous post, 'ASC' for next.
     */
    $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
 
    $query     = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
    $query_key = 'adjacent_post_' . md5( $query );
    $result    = wp_cache_get( $query_key, 'counts' );
    if ( false !== $result ) {
        if ( $result ) {
            $result = get_post( $result );
        }
        return $result;
    }
 
    $result = $wpdb->get_var( $query );
    if ( null === $result ) {
        $result = '';
    }
 
    wp_cache_set( $query_key, $result, 'counts' );
 
    if ( $result ) {
        $result = get_post( $result );
    }
 
    return $result;
}


Top

Changelog 

Changelog
Version Description
2.5.0 Introduced.

Top ↑

Was this article helpful?
Dislike 0
Views: 9