[wp-hackers] Flushing get_pages cache

Howard Fore howard.fore at gmail.com
Sun Mar 6 17:16:28 GMT 2005


Hi,

I'm using the Pages functionality pretty extensively for a new site
I'm building. It's largely static content, not very bloggish so the
Pages stuff works great. The problem is that I'd like to use the
wp_list_pages function twice on the page with different parameters.
The first call creates a top level navigation bar, just getting the
first level of pages:

        wp_list_pages('sort_column=menu_order&depth=1&exclude=10&title_li=');

Then later on in the sidebar, I'd like to show the pages that are in
the same "section" of the current page. I'm defining the section as
the group of Pages that are children of the topmost page that is a
parent of the current page. IE:

        0 (main main)
        - page A
        -- page A1
        -- page A2
        -- page A3
        --- page A3-1
        --- page A3-2
        - page B
        - page C

Pages A* are all in the same section, page B and page C are each
separate sections.

I wrote a little plugin that walks up the tree from the current page
to find the top most page called get_top_page_id.  Then I can do this:

        wp_list_pages('title_li=&sort_column=menu_order&child_of=' .
get_top_page_id())

But I'd also like to exclude the current page from the list, so I
added the current page:

        global $id;
        wp_list_pages('title_li=&sort_column=menu_order&child_of=' .
get_top_page_id() . '&exclude=' . $id)

(I tried several different methods of getting the current page ID and
the global var was the only one that seemed to work.)

But this still didn't get me to where I wanted to go. After liberally
sprinkling some log statements, I traced the issue to the get_pages
function. This is called by wp_list_pages and has a caching mechanism.
I assume this is so if more than one function in a page request needs
to use get_pages that only one database call would be made. Fine,
except that I'm breaking the assumption that the page tree would be
the same for each call. Remember on the first call I just want the top
level Pages, but on the second call I want a subtree with a specific
Page removed.

So this is what I tried:

I changed get_pages from

        function get_pages($args = '') {
            global $wpdb, $cache_pages;
        
            if (!isset($cache_pages) || empty($cache_pages)) {
            parse_str($args, $r);

 to 

        function get_pages($args = '') {
            global $wpdb, $cache_pages;
        
            parse_str($args, $r);
            if (!isset($cache_pages) || empty($cache_pages) ||
(isset($r['flush_cache']) && $r['flush_cache'] == 'y')) {

This allows me to use a new parameter of "flush_cache" on my second
call and the request works as I desired.

Is this a useful addition to anyone but me?

Now, I'd like to point out that I'm a newbie at Wordpress hacking, if
I've broken some sacred WP principle that I missed becuase I just
joined the list, please give me a pointer to the discussion.

Thanks.

--
Howard Fore, howard.fore at gmail.com


More information about the wp-hackers mailing list