WordPress.org

Ready to get started?Download WordPress

Forums

Script help: Get Array of Page Parents (9 posts)

  1. jpepper
    Member
    Posted 9 years ago #

    I hope someone can help me out with this challenge. It is related to a previous post of mine (http://wordpress.org/support/topic/32310).

    I need a function that will return an array of a page's hierarchy. Basically I want an array containing each of a page's parent's IDs.

    Food (id=5)
    - Desserts (id=7)
    - - Cookies (id=9)
    - - - Chocolate Chip (id=12)

    So if I was on the Chocolate Chip page, a call to the function would return an array containing the following: (5, 7, 9).

    Thanks so very much for your help.

  2. jpepper
    Member
    Posted 9 years ago #

    Anyone?

  3. jpepper
    Member
    Posted 9 years ago #

    Ok. How about a bounty of $20 (US) for the first person that can provide the above described funtion. I'll pay via PayPal. If you do decide to take me up on this offer, please post and let the board know that you are working on it. Thanks!

  4. Kafkaesqui

    Posted 9 years ago #

    $2o? Seems a lot for so little code. See if this works:

    <?php
    $parent_array = array();
    $current_page = $post->ID;
    $parent = 1;

    while($parent) {
    $page_query = $wpdb->get_row("SELECT ID, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $parent = $current_page = $page_query->post_parent;
    if($parent)
        $parent_array[] = $page_query->post_parent;
    }
    ?>

    $parent_array now holds what you're after. If run outside The Loop you may need to initialize the $post array; to do that make this the first line of your code:

    $post = $wp_query->post;

    Now about that $20... Donate the money to a worthy cause. I would suggest the American Cancer Society:

    https://www.cancer.org/asp/donate/don_multi_donate.asp

  5. Kafkaesqui

    Posted 9 years ago #

    I missed the part about needing it as a function:

    <?php
    function page_parents() {
    global $post, $wp_query, $wpdb;
    $post = $wp_query->post;

    $parent_array = array();
    $current_page = $post->ID;
    $parent = 1;

    while($parent) {
    $page_query = $wpdb->get_row("SELECT ID, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $parent = $current_page = $page_query->post_parent;
    if($parent)

        $parent_array[] = $page_query->post_parent;
    }

    return $parent_array;
    }
    ?>

  6. jpepper
    Member
    Posted 9 years ago #

    Thanks so much Kafkaesqui. Let me test it and I'll get back to you shortly. Again thanks!

  7. jpepper
    Member
    Posted 9 years ago #

    Kafkaesqui. Your solution works great! Thank you. I will make a donation on your behalf to the ACS.

    I hope someone else finds this function useful.

  8. paulfynch
    Member
    Posted 8 years ago #

    Brilliant!
    Thank you for your contribution.
    God, I love WordPress, it has simplified my life to no end :D

    Cheers!
    PF

  9. paulfynch
    Member
    Posted 8 years ago #

    Oh,
    I don't have much cash right now, but when I pay down my CC i'll make that donation too ;)
    PF

Topic Closed

This topic has been closed to new replies.

About this Topic