Support » Fixing WordPress » template-functions-post.php foreach error

  • Hello,

    Pages and especially sub-pages have began acting problematically.

    1.) When I click on a Page, the page loads but all links to all other pages vanish.

    2.) If I click on a sub-page, the page loads but the following error is produced in the location where the links to each page would normally appear:

    Warning: Invalid argument supplied for foreach() in /home/mysknet/public_html/wp-includes/template-functions-post.php on line 368

    This error occurrs in the theme that I was previously using, so it’s not likely related to the newest theme that I’ve began using.

    The only other two changes that come to mind are:

    1.) I deleted the Classic theme. Never likely to use it.

    2.) I’ve began using a perma link structure as per the Mod_Rewrite Trackback Spam Blocker suggested in the codex and found at this location:

    Other than that I can think of no meaningful changes. I have certainly not made any changes to the template-functions-post.php file.

    My blog can be found at


Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m updating this thread with additional information that I hope will help diagnose the problem.

    I have switched between all of the themes that I have installed and have found one theme out of all of them that does not create the error.

    The default theme will not show any of the problems listed in my original post. The other themes – which have links for the pages – do produce the error.

    Additionally, I have re-uploaded the classic theme “just in case” and have found this to not fix the problem. The classic theme does produce the error as well.

    edit: do != does, typo fix

    I don’t suppose that I can get some help with this? Tips, links, anything?

    Hmm… Real useful bunch here.


    I have the same problem… Haven’t figured out how to fix it yet. If you do please post the solution here.

    Interestingly, I only seem to get this error when I click on the link in the Pages list in the sidebar that in Page Management in the Admin Control Panel looks like

    � � NYC Press cuttings

    but has subpages beneath it, eg

    � � � The Evening Post, Bristol January 2002
    � � � The Guardian, August 2000

    But I don’t get the error with the other second-level pages (ie those with no subpages).


    Later: I’ve also realised that I get the error using my own page template file (although it works fine with the default page template).

    I can’t see that it’s got anything to do with the code in my template file, which is cut and paste from the page.php file with a suitable

    Template Name: NYC_Press_Listing

    in place at the top.

    I simply wanted the page to display an introduction (written in the Page Content, and then to list the children of the page with ID of 25:

    <?php wp_list_pages ('child_of=25&sort_column=ID&title_li='); ?>

    But even when I have a completely empty template file, eg

    Template Name: NYC_Press_Listing
    <?php get_header(); ?>
    <div id="content">
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    I get the same error.

    Looking into this matter more closely I discovered that I would get the error if I was using my own page template, but not if I was using the default page template (which was odd since my template was simply a cut-and-paste of the default page, with a few minor additions; and it would even throw up the error if I had a completely empty #content div!).

    I wondered if it was indeed to do with the template-functions-post.php file, so backed up the latest version and copied over a version from the first WP1.5 installation I downloaded (the file is 12KB and dated 2005-02-15 at 01:21) and … it worked! I get no error now.

    I’ve run the two files through a file-comparison application (the freeware ExamDiff) and these are the lines that have changed:

    Line 262 used to have:

    function get_pages($args = '') {
    global $wpdb;

    and is now

    function get_pages($args = '') {
    global $wpdb, $cache_pages;

    if (!isset($cache_pages) || empty($cache_pages)) {

    Then further on line 292 (in the latest) file has an addition. What was "ID, post_title,post_parent " . is now "ID, post_title, post_name, post_parent " .

    A further (larger) addition, around line 300. In the old file:

    return $pages;


    $cache_pages = array();
    if (count($pages)) {
    foreach($pages as $page) {
    $cache_pages[$page->ID] = $page;

    return $cache_pages;

    And finally, around line 330 in the new file (319 in the old):

    $page_tree[$page->ID][‘title’] = $page->post_title;
    $page_tree[$page->ID][‘name’] = $page->post_name;

    The latter line there is new. The new file is 401 lines long, the old 390. I’m guessing that the problem lies with the addition of the new code.

    I’ve read elsewhere on this forum that someone reckoned the error to lie in the if (!isset($cache_pages) || empty($cache_pages)) { line.

    That’s all from me tonight.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘template-functions-post.php foreach error’ is closed to new replies.