I’m trying to create a page template that outputs the page content first, and then links to the subpages of that page. I can’t use
wp_list_pages()though, because I need to use some custom fields from those subpages.
So I get those pages with a custom SELECT query directly from the database and create a second loop after the first one, as per these instructions on the Codex.
The problem is this: inside the second loop, template tags like
the_permalink()still output their values for the parent page, instead of the subpage the loop is working on at that point. Things like
the_ID()do contain the right values however.
Does anyone have any idea why this is, and how I can fix or work around it?
Or is there perhaps an easier way to to this that I’m not aware of?
Thanks in advance.
Yeah, read that too. It uses
query_posts(). If I could use that, it would indeed probably solve my problem. But I can’t seem to find a
page_parentparameter for it.
Well, after a lot of trying and faling, I finally got it to work. Right underneath the page loop, I make my custom query:
$parent = 55; $sql = sprintf( 'SELECT * FROM %s WHERE post_status = "publish" AND post_type = "page" AND post_parent = %d ORDER BY menu_order', $wpdb->posts, $parent ); $pluginPages = $wpdb->get_results($sql, OBJECT);
And then create a loop for it:
if($pluginPages):foreach($pluginPages as $pluginPage): setup_postdata($pluginPage); $title = get_the_title($pluginPage->ID); $permalink = get_permalink($pluginPage->ID); // some get_post_meta() calls here // make a nice blog-like list of sub pages here endforeach;endif;
And the result can be seen here.
Instead of doing that, using the get_pages() function might make a bit more sense. It can’t limit based on post_parent, but you can filter by that after retrieving the data.
The advantage is that the data this way will be cached, instead of making a database hit every single time.
WordPress does caching by itself? I thought one needed a plugin like WP Cache for that?
- The topic ‘Problems with second loop and custom SELECT query’ is closed to new replies.