1

I'm trying to get a list of all pages on a wordpress site and access their ID. I can get a list of pages using the get_pages() function. When I try to access any of the elements with their keys, it doesn't interpret.

For example, the following code wouldn't work:

$pages = get_pages();

foreach ($pages as $page) {
  $ID = $page['ID'];
}

But, if I do this:

$pages = get_pages();

foreach($pages as $page) {
  foreach ($page as $key => $value) {
    echo("<p>$key</p>");
  }
}

It clearly prints out ID as a valid key.

What am I doing wrong?

2
  • 1
    Please provide the error message, and a var_dump or pages. Commented Aug 8, 2017 at 9:06
  • 1
    See the manual The returned array is an array of "page" objects The manual is always a good place to look when things go wrong. So $ID = $page->ID; is probably what you want Commented Aug 8, 2017 at 9:12

3 Answers 3

3

When you do this

$pages = get_pages();

foreach($pages as $page) {
  foreach ($page as $key => $value) {
    echo("<p>$key</p>");
  }
}

So the above $pages becomes associated array, and you can access key with associative array. Associated arrays are used in php for KEY VALUE PAIR

So you can use

$pages = get_pages(); 
  foreach ( $pages as $page ) {
  echo  $page->ID;

}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, works perfectly. When I create my own array by declaring my own keys for elements I can access it with the normal array syntax. How is that different to this example?
I dint got you... elaborate more please
See @Pawan Kumar answer.
@RiggsFolly Thank you.
Thanks for accpeting my solution. I hope it helped everyone in this case.
3

Because get_pages() returns an array of "pages" OBJECTS $page in the foreach loop will be a page object therefore you need to use the -> to address the ID property.

$pages = get_pages(); 
foreach ( $pages as $page ) {
    echo  $page->ID;
}

4 Comments

Do or Do Not, there is no "Try" padawan. A good answers will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer.
Ok . Thanks @RiggsFolly.
Now it is better. Thank you again. @RiggsFolly.
Thank you. I thought that it was returning an array of arrays, not objects. Dumb mistake.
-1

Give an example of the array(multidimensional,key->value,simple array,etc). Let's assume:

//simple array parsing
$pages = array('one','two','three');
foreach ($pages  as $page) {
  echo "Current value: $page.\n";
}

//key-> value parsing
$pages   = array(
"one" => 1,
"two" => 2,
"three" => 3
);

foreach ($pages as $k => $v) {
  echo "\$pages[$k] => $v.\n";
}

 //multidimensional array parsing
 $pages = array();
 $pages[0][0] = "one";
 $pages[0][1] = "two";
 $pages[1][0] = "three";

 foreach ($pages as $v1) {
    foreach ($v1 as $v2) {
       echo "$v2\n";
    }
 }

See references on [link]http://php.net/manual/en/control-structures.foreach.php

1 Comment

Thank you for the answer. I got the whole situation wrong though, the array actually contained objects, not arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.