WordPress.org

Ready to get started?Download WordPress

Forums

Problems with second loop and custom SELECT query (7 posts)

  1. Colin Helvensteijn
    Member
    Posted 6 years ago #

    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_title() and the_permalink() still output their values for the parent page, instead of the subpage the loop is working on at that point. Things like get_post_meta() and 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.

  2. MichaelH
    Member
    Posted 6 years ago #

    Take a look at The Loop-Multiple Loops.

  3. Colin Helvensteijn
    Member
    Posted 6 years ago #

    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_parent parameter for it.

  4. Colin Helvensteijn
    Member
    Posted 6 years ago #

    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.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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.

  6. Colin Helvensteijn
    Member
    Posted 6 years ago #

    WordPress does caching by itself? I thought one needed a plugin like WP Cache for that?

  7. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Yes, WordPress has a built-in object cache. It does memory caching of certain types of things, and can cache object data to disk as well, if you enable it in the wp-config.php file.

Topic Closed

This topic has been closed to new replies.

About this Topic