Switch language

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




_get_post_ancestors [ WordPress Function ]

_get_post_ancestors ( $_post )
Access:
  • private
Parameters:
  • (object) $_post Post data.
Uses:
  • $wpdb
Returns:
  • (null) When nothing needs to be done.
Defined at:



Retrieve post ancestors and append to post ancestors property.

Will only retrieve ancestors once, if property is already set, then nothing will be done. If there is not a parent post, or post ID and post parent ID are the same then nothing will be done.

The parameter is passed by reference, so nothing needs to be returned. The property will be updated and can be referenced after the function is complete. The post parent will be an ancestor and the parent of the post parent will be an ancestor. There will only be two ancestors at the most.

Source


<?php
function _get_post_ancestors(&$_post) {
    global 
$wpdb;

    if ( isset(
$_post->ancestors) )
        return;

    
$_post->ancestors = array();

    if ( empty(
$_post->post_parent) || $_post->ID == $_post->post_parent )
        return;

    
$id $_post->ancestors[] = (int) $_post->post_parent;
    while ( 
$ancestor $wpdb->get_var$wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1"$id) ) ) {
        
// Loop detection: If the ancestor has been seen before, break.
        
if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
            break;
        
$id $_post->ancestors[] = (int) $ancestor;
    }
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



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