Support » Fixing WordPress » BUG: Unable to distinguish a childless parent with new wpdb?

  • I have been working with an older version of the Sidebar code and have been trying to figure out how one determines if a page has no children.

    One of the standard methods proposed has been the following line (on all widget based sidebars I have seen):

    if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>

    Where the assumption is: if a page has no children, there will be no pages with the parent’s ID in the parent_ID variable. This was because the parent_ID attribute would only be set if there was a child page.

    BUT, with the newer versions of WP, two things seem to have changed.

    • new pages seem to have the parent_ID set to themselves
    • when a page is revised, old versions of pages are stored in the database

    Now, since the page itself seems to have parent_ID set to being it’s own page_ID (or post_ID), this code does not work. Because now – this codeline never goes NULL.

    Any suggestions???

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m not seeing Pages get assigned a post_parent value when I write that page.

    Test for post_status=’publish’ and post_type=’page’ to exclude revisions and such.

    Michael –

    Thanks for the response – I spent a load of time on this and researching various functions. Let me show you the code so you can see what I mean:

    <?php global $notfound; ?>
    	<?php /* Creates a menu for pages beneath the level of the current page */
      		if (is_page() and ($notfound != '1')) {
       		$current_page = $post->ID;
       		while($current_page) {
        		$page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
        		$current_page = $page_query->post_parent;
       		$parent_id = $page_query->ID;
       		$parent_title = $page_query->post_title;
    		if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
    			<div class="sidebar_box">
    			<h3>In This Section</h3>
    			<ul><?php wp_list_pages('sort_column=menu_order&title_li=&child_of='. $parent_id); ?></ul>
        	<?php if ($parent_id != $post->ID) { ?>
         		<a href="<?php echo get_permalink($parent_id); ?>" style="margin-top: 10px;"><?php printf(__('Back to %s'), $parent_title ) ?></a>
        	<?php } ?>
     	<?php } } ?>
     	<?php if (is_attachment()) { ?>
     			<div class="sb-pagemenu">
       				<a href="<?php echo get_permalink($post->post_parent); ?>"
    				rev="attachment"><?php printf(__('Back to \'%s\''), get_the_title($post->post_parent) ) ?></a>
    <?php } ?>
    <?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>
    <?php endif; ?>

    The difficulty is that in the database, the line that is the conditional is never going NULL or false. I have spent time figuring out the post_parent and other factors – but can not decypher how best to resolve this.

    I tried this:

    if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status='publish' AND post_type='page'")) { ?>

    and now it works for the pages. My only fear was what happens when we have posts instead of pages.

    I also tried using the new get_children() function – but it only returns the same value no matter what.

    Published posts, though with post_status=”publish”, will have post_type=”post”.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘BUG: Unable to distinguish a childless parent with new wpdb?’ is closed to new replies.