Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_adjacent_post [ WordPress Function ]

get_adjacent_post ( $in_same_cat = false, $excluded_categories = '', $previous = true )
Parameters:
  • (bool) $in_same_cat Optional. Whether post should be in a same category.
  • (array|string) $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
  • (bool) $previous Optional. Whether to retrieve previous post.
Returns:
  • (mixed) Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
Defined at:



Retrieve adjacent post.

Can either be next or previous post.

Source


<?php
function get_adjacent_post$in_same_cat false$excluded_categories ''$previous true ) {
    global 
$post$wpdb;

    if ( empty( 
$post ) )
        return 
null;

    
$current_post_date $post->post_date;

    
$join '';
    
$posts_in_ex_cats_sql '';
    if ( 
$in_same_cat || ! empty( $excluded_categories ) ) {
        
$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";

        if ( 
$in_same_cat ) {
            
$cat_array wp_get_object_terms($post->ID'category', array('fields' => 'ids'));
            
$join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" implode(','$cat_array) . ")";
        }

        
$posts_in_ex_cats_sql "AND tt.taxonomy = 'category'";
        if ( ! empty( 
$excluded_categories ) ) {
            if ( ! 
is_array$excluded_categories ) ) {
                
// back-compat, $excluded_categories used to be IDs separated by " and "
                
if ( strpos$excluded_categories' and ' ) !== false ) {
                    
_deprecated_argument__FUNCTION__'3.3'sprintf__'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
                    
$excluded_categories explode' and '$excluded_categories );
                } else {
                    
$excluded_categories explode','$excluded_categories );
                }
            }

            
$excluded_categories array_map'intval'$excluded_categories );

            if ( ! empty( 
$cat_array ) ) {
                
$excluded_categories array_diff($excluded_categories$cat_array);
                
$posts_in_ex_cats_sql '';
            }

            if ( !empty(
$excluded_categories) ) {
                
$posts_in_ex_cats_sql " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" implode($excluded_categories',') . ')';
            }
        }
    }

    
$adjacent $previous 'previous' 'next';
    
$op $previous '<' '>';
    
$order $previous 'DESC' 'ASC';

    
$join  apply_filters"get_{$adjacent}_post_join"$join$in_same_cat$excluded_categories );
    
$where apply_filters"get_{$adjacent}_post_where"$wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql"$current_post_date$post->post_type), $in_same_cat$excluded_categories );
    
$sort  apply_filters"get_{$adjacent}_post_sort""ORDER BY p.post_date $order LIMIT 1" );

    
$query "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
    
$query_key 'adjacent_post_' md5($query);
    
$result wp_cache_get($query_key'counts');
    if ( 
false !== $result )
        return 
$result;

    
$result $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
    if ( 
null === $result )
        
$result '';

    
wp_cache_set($query_key$result'counts');
    return 
$result;
}
?>

Examples [ wp-snippets.com ]

Google Arama Sonuçlarý

Dahasý ...

0 User Note(s)

Henüz yok. Ýlk sen ol!

Yeni Ekle ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics