• 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?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Same thing here…

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

    Looking forward to a solution.

    Txs!

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

    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)

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘ancestors on local machine but not on server’ is closed to new replies.