Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 703955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:53:17+00:00 2026-05-14T03:53:17+00:00

The code below is resulting in an error on a site in which there

  • 0

The code below is resulting in an error on a site in which there are ~ 1500 posts. It performs fine when post count is nominal, however, this heavy load is exposing the weakness of the code and I’d like to optimize it.

Interestingly, when I disable this menu and instead use the “Recent Posts” widget, the posts are drawn fine. So I’d probably do good to borrow from that code if I knew where to find it, or better yet, If I could call the widget directly in my theme, passing it a post count variable.

Fatal error: Allowed memory size of
33554432 bytes exhausted (tried to
allocate 16384 bytes) in
/home1/est/public_html/mysite/wp-includes/post.php
on line 3462

The code is below. Its purpose is to list “recent posts”.

global $post; 
$cat=get_cat_ID('myMenu'); 
$cathidePost=get_cat_ID('hidePost'); 
$myrecentposts = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => $count-of-posts));
$myrecentposts2 = get_posts(array('post_not_in' => get_option('sticky_posts'), 'cat' => "-$cat,-$cathidePost",'showposts' => -1));
$myrecentpostscount = count($myrecentposts2);

if ($myrecentpostscount > 0) 
{ ?>
    <div class="recentPosts"><h4><?php if ($myHeading !=="") { echo $myHeading; } else { echo "Recent Posts";} ?></h4><ul>
    <?php 
    $current_page_recent = get_post( $current_page );
    foreach($myrecentposts as  $idxrecent=>$post) {
        if($post->ID == $current_page_recent->ID)
            {
                $home_menu_recent = ' class="current_page_item';
            } 
            else 
            {
                $home_menu_recent = ' class="page_item';
            }
            $myclassrecent = ($idxrecent == count($myrecentposts) - 1 ? $home_menu_recent.' last"' : $home_menu_recent.'"'); 
        ?>
    <li<?php echo $myclassrecent ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php } ; if (($myrecentpostscount > $count-of-posts) && $count-of-posts > -1){ ?><li><a href="<?php bloginfo('url'); ?>/recent">View All Posts</a></li><?php } ?></ul></div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:53:17+00:00Added an answer on May 14, 2026 at 3:53 am

    Don’t take this the wrong way. I’m not going to solve your memory problem at all in this answer. I will post another answer with a reason I believe you could have the mem problem The following is just a suggestion I hope you take as helpful: You have so much html jumbled in between your layout logic it is going to take you 3x as long to do figure out what is breaking and why.

    Following the principles of refactoring I’m going to give one example and show how you can clean this up to the point where you can actually debug it yourself.

    I’m taking this section of code to refactor a bit:

    if ($myrecentpostscount > 0) 
    { ?>
        <div class="recentPosts"><h4><?php if ($myHeading !=="") { echo $myHeading; } else `{ echo "Recent Posts";} ?></h4><ul>`
    

    First, pull the logic out of the template display (reformat for readability at the same time:

    if ($myrecentpostscount > 0)
    {
        if ($myHeading !=="") 
        {
            $displayHeading =  $myHeading; 
        } 
        else 
        { 
            $displayHeading =  "Recent Posts";
        }
    

    ?>

    Second, replace temporary variables with function calls [$myHeading]

    /**
    * This function determines if a heading is null, and returns the default if it is.
    */
    function getDisplayHeading($customHeading)
    {
        if ($customHeading == "") 
        { 
            return "Recent Posts";
        }
        return $customHeading;
    }   
    
    if ($myrecentpostscount > 0)
    {
        ?>
        <div class="recentPosts"><h4>
        <?php echo getDisplayHeading($myHeading); ?></h4><ul>
    

    Now finally, when you have all your logic at the top of the page, you can profile each function to see how much memory it uses and at which point too much memory is used in your page request. I’m going to post another answer to try to help there though, because I believe I have an idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code below allows me to find the word error in all my files
The code below fails on the line: Class.forName(oracle.jdbc.driver.OracleDriver); with the error: java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver The
The code below works well right now; however, it's serially executed. I'd like to
Can anyone help me understand why my code (see below) is resulting in the
I'm using the code below to render a preview of a PDF page. However
OK, so this code below sometimes crashes on the line below //bug!! The error
The code below does not compile. The compiler error is Cannot implicitly convert type
Code below is working well as long as I have class ClassSameAssembly in same
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.