WordPress.org

Ready to get started?Download WordPress

Forums

ancestors on local machine but not on server (5 posts)

  1. bmsterling
    Member
    Posted 6 years ago #

    Hey all,
    trying not to go crazy here but I am trying to figure something out. On my local test server when I take a look at the $post variable I get:

    [ancestors] => Array
            (
                [0] => 1
            )

    But when I upload the same exact code, and the same exact data (via phpmyadmin) I don't get the ancestors array, am I missing something? There really should not be a difference if the code and the data is exactly the same correct?

  2. amscultori
    Member
    Posted 5 years ago #

    Same thing here...

    In my local server, $post->ancestors show up, but not on remote server

    Looking forward to a solution.

    Txs!

  3. Matt McInvale
    Member
    Posted 5 years ago #

    I have an install on an unfamiliar server that's having the problem. Has anyone made any progress on this? Quite bizarre.

  4. e_anka
    Member
    Posted 5 years ago #

    I have the same problem.
    I have written a function that resolves the problem. It's not perfect, because it sends multiple requests to find post parent, so I am afraid it's quite slow, but anyway it works.

    function get_ancestors ($id) {
        $p = get_post ($id);
        $parent = $p->post_parent;
        $ancestors = array ();
        while ($parent != '0') {
            $ancestors[] = $parent;
            $p = get_post ($parent);
            $parent = $p->post_parent;
        }
        return $ancestors;
    }

    You can put the function into your plugin file, or functions.php of your template file and use it like:
    $ancestors = get_ancestors (post_id)
    where post_id is the id of the post of course ;)
    Hope this helps:)

    Edit:
    After a while I realized you can also pass whole $post as an argument - and shorten everything by one request. The code will look like:

    function get_ancestors ($p) {
        $parent = $p->post_parent;
        $ancestors = array ();
        while ($parent != '0') {
            $ancestors[] = $parent;
            $p = get_post ($parent);
            $parent = $p->post_parent;
        }
        return $ancestors;
    }

    And usage: $ancestors = get_ancestors ($post)

  5. anonyme
    Member
    Posted 5 years ago #

    Salut, je viens d'avoir le même problème. Ne voulant pas allourdir mon code j'ai cherché un peu dans le fichier post.php. On y remarque qu'il existe la fonction suivante :

    get_post_ancestors($post);

    Elle retourne exactement la(es) même(s) valeur(s) que $post->ancestors

    Bonne journée.

Topic Closed

This topic has been closed to new replies.

About this Topic