Wordpress: Fixing Wordpress 2.5 Child Link System

Posted 3 months ago at 4:39 pm. 21 comments

So you’ve just upgraded to Wordpress 2.5 and all of a sudden you are greeted with an error that says something like this:

Warning: Invalid argument supplied for foreach() in wp-includes/post.php in line 1783

It’s something like that but basically this error only occurs if your page template tries to access a child list using the following:

1
$childPages = get_page_children($page->ID, '');

It doesn’t really matter about any of the code other than the last bit. This worked before because the function get_page_children realized it was empty and replaced it with the right value it’s self. Well it doesn’t anymore so here is how you fix it.

To fix it we need to add a little to the code, it’s only one line of code but it makes all the difference. Now it may be slightly different depending on your template but it should be easy enough to modify it to match. To fix it you need to alter the code to the following:

1
2
$pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
$childPages = get_page_children($page->ID, $pages);

Now that should fix it. It’s a simple fix really, for those who want to know the technical part here it comes.

Basically the code used to read a blank value and replace it with the values it expected from a page cache, well that bit has been removed so we will work around it. The value it expects is an array with each of the pages you want to checks object inside it. Each page it gets it uses $page->page_parent to check to see if it is the child of the current page.

So we have to give it the value that it expects. To do this we use a function built into Wordpress to give us a pre-built array with each pages object in it. Then we hand it to the get_page_children function in place of the blank space.

Hopefully this should fix the problem in most if not all templates. Remember it will only happen if there is a blank value in the second part of the get_page_children function. Give me a shout if you have any problems, just write in the code your template uses to access the get_page_children function and I’ll try and help you fix it. If you do leave me some code to check please use the code colorer by writing the code in a <pre lang=”php” line=”1″> tag and it will automatically color the code and make it easier to read. Thanks. :)

EDIT: To make it slightly clearer. If you need help with fixing this you will need to provide me with the code in your template that calls get_page_children() not the actual function which is on page 1783 of wp-includes/post.php. Usually the code I need is located in page.php in your template folder but sometimes it is different. Sorry about that. :’(

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • StumbleUpon
  • Google
  • Furl
  • del.icio.us
  • Facebook

21 Replies

  1. Hi. Here is the code my theme uses. What do I need to change please. And thanks.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    function &get_page_children($page_id, $pages) {
    	$page_list = array();
    	foreach ( $pages as $page ) {
    		if ( $page->post_parent == $page_id ) {
    			$page_list[] = $page;
    			if ( $children = get_page_children($page->ID, $pages) )
    				$page_list = array_merge($page_list, $children);
    		}
    	}
    	return $page_list;
    }
  2. Hi Matt,

    Sorry, I haven’t made myself clear about which code I need. I will alter the post to make it a little clearer.

    You will need to provide me with the code that calls the get_page_children() function and not the actual function itself which is found on line 1783 of wp-includes/post.php.

    You’re looking for something that looks like this:

    1
    
    $childPages = get_page_children($page->ID, '');

    I am unsure of what page it will be on as it is may be different for your theme, but most including mine is located on page.php.

    My best guess at what you will need to do though is find the above code and change it to this:

    1
    2
    
    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
    $childPages = get_page_children($page->ID, $pages);

    If not then just post the code you can find here and I’ll see what I can do. Sorry for not being clear, I have promptly slapped myself and am fixing the post now.

  3. Johan Gravatar Apr 7th 2008

    Sorry for the previous two posts! Can’t seem to type today… Thanx for finding the fix to this problem… can’t seem to get it working in mine though. Here’s my code snippet:

    1
    2
    3
    4
    5
    6
    
    	if ($aOptions['show_subpages']) :
     
    		global $id;
     
    		$aSubPages = get_page_children($id, '');
    		if (count($aSubPages) > 0) :
  4. Hi Johan,

    It can be tricky to do as it depends on the template. From what I can see though it is fairly similar to some I have seen.

    Take this code:

    1
    2
    
    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
    $aSubPages = get_page_children($id, $pages);

    and paste it over the top of the line in your code that says:

    1
    2
    3
    4
    5
    6
    
    	if ($aOptions['show_subpages']) :
     
    		global $id;
     
    		$aSubPages = get_page_children($id, '');
    		if (count($aSubPages) > 0) :

    That should fix your problems. If not feel free to give me another buzz and I’ll help you some more.

    Thanks. :D

  5. I had the same issue as Johan. I placed the above code in and it fixed the problem. Thanks for the help and for your time.

  6. @Matt: No probs. ;)

  7. Hey,

    So i’m pretty retarted at coding and PHP, so forgive me if you aslready answered this one, but i can’t figure it out…

    I have:

    1
    2
    3
    4
    5
    
    get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
    		$aSubPages = get_page_children($id, $pages);
    		if (count($aSubPages) > 0) :
     
    ?>

    Can you please let me know what that would look like after i make changes, so i can just copy and paste the whole thing? thanks!

  8. Hey Ross,

    It’s actually very close but your code should look like this:

    1
    2
    3
    4
    5
    
    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
    		$aSubPages = get_page_children($id, $pages);
    		if (count($aSubPages) > 0) :
     
    ?>

    Hope that works for you. ;)

  9. Johan Gravatar Apr 9th 2008

    I’m glad it worked for Matt… but it seems mine is still broken. Here is the full code for the page.php file. Maybe my theme is doing something weird… thanks again for the help! (Again, sorry for the multiple postings…)

    Veneficus Unus: Sorry but half the code went missing. Could you please post code in tags like this <pre lang=”php” line=”1″> and </pre> thank you.

  10. Seems WP wants to execute some of the code in the template even using the tags. Here’s a link to a txt file with my code in it.

  11. @Johan: Ok so I think I know what you need to do. Thanks for using a txt file. I’ll have to look into why WP kept executing the code, the <pre> tags should have stopped it.

    Anyway find the code in your txt file that looks like this:

    1
    
    $aSubPages = get_page_children($id, '');

    and replace it with this:

    1
    2
    
    $pages = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_type = 'page'" ) );
    $aSubPages = get_page_children($id, $pages);

    Hopefully that should work. If not give me another shout and I’ll see if I can find another problem. ;)

  12. It works! It works! Thanks so much for your time and patience. =)

  13. No worries. :)

  14. Thanks heaps for this! Worked a treat :)

  15. No problem Tim. ;)

  16. Great Job Here…I enjoyed it..! Gary

  17. Thanks. :)


Leave a Reply