Forums

Excluding Multiple Pages with (exclude_tree) (11 posts)

  1. dcbennion
    Member
    Posted 2 years ago #

    I am using the wp_list_pages function in (Version 2.8.4) and having issues excluding multiple post IDs. I read documentation and 'fixes' all over the place for 2.7 and tried a couple that supposedly sove the problem in 2.8, to no avail. At this point I can only exclude one ID at a time using the exclude_tree argument.

    Anyone else have this problem or know of a fix?

    My Code:

    $args = array(
    'depth'        => 0,
    'show_date'    => "",
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => "",
    'include'      => "",
    'title_li'     => __(''),
    'echo'         => 1,
    'authors'      => "",
    'sort_column'  => 'menu_order, post_title',
    'link_before'  => "",
    'link_after'   => "",
    'exclude_tree' => 31,33,18,74,); 
    
    wp_list_pages( $args );
  2. jonradio
    Member
    Posted 2 years ago #

    Two things of interest to you:

    1. exclude_tree does not work properly, and no fix is scheduled until Version 2.9, ref. - http://core.trac.wordpress.org/ticket/8683
    2. exclude_tree must specify a string, ref. - http://codex.wordpress.org/Template_Tags/wp_list_pages

    Your coding 'exclude_tree' => 31,33,18,74,); is very, very clearly wrong. What you have is an assignment of an integer 31 to exclude_tree, followed by four more array elements, presumably assigned to $args[0], [1], [2] and [3], the integers 33, 18 and 74, and a null value.

    So, I would definitely try correcting that first, to see if it will work, or you are going to be hampered by the bug.

    The correct coding is:
    'exclude_tree' => '31,33,18,74');

  3. Mark / t31os
    Moderator
    Posted 2 years ago #

    You could create a work-around in the mean time, use get_col to grab the IDs for all posts from your list of parent IDs, then load them into the exclude parameter (along with the parent IDs if needed), which does still work.

    You'd end up with a big exclude list, but it should work the same, it'll just require a little more code.

  4. Technokinetics
    Member
    Posted 2 years ago #

    [Removed to test code]

  5. Technokinetics
    Member
    Posted 2 years ago #

    Try this (where the page IDs that you would normally pass to exclude_tree go in the first line):

    $parent_pages_to_exclude = array(31,33,18,74);
    foreach($parent_pages_to_exclude as $parent_page_to_exclude) {
      if ($page_exclusions) { $page_exclusions .= ',' . $parent_page_to_exclude; }
      else { $page_exclusions = $parent_page_to_exclude; }
      $descendants = get_pages('child_of=' . $parent_page_to_exclude);
      foreach($descendants as $descendant) {
        $page_exclusions .= ',' . $descendant->ID;
      }
    }
    wp_list_pages('title_li=&sort_column=menu_order&exclude=' . $page_exclusions);

    - Tim

  6. jtimar
    Member
    Posted 2 years ago #

    tim, can you clarify where that cose should be inserted for a PHP novice? It creates a parse error for me.

  7. eggnog
    Member
    Posted 2 years ago #

    jtimar,

    All you have to do is include Tim's block of code in your template the same way you would the regular <?php wp_list_pages();?> tag.

    It would look like this:

    <?php $parent_pages_to_exclude = array(31,33,18,74);
    foreach($parent_pages_to_exclude as $parent_page_to_exclude) {
    if ($page_exclusions) { $page_exclusions .= ',' . $parent_page_to_exclude; }
      else { $page_exclusions = $parent_page_to_exclude; }
      $descendants = get_pages('child_of=' . $parent_page_to_exclude);
      foreach($descendants as $descendant) {
        $page_exclusions .= ',' . $descendant->ID;
      }
    }
    wp_list_pages('title_li=&sort_column=menu_order&exclude=' . $page_exclusions); ?>
  8. visaap
    Member
    Posted 2 years ago #

    This is very usefull!
    Thanx Technokinetics

  9. JaredPenner
    Member
    Posted 2 years ago #

    How would I use this code to display the children on one page but exclude the grandchildren of that same page?

  10. sreagan
    Member
    Posted 2 years ago #

    Hello,
    I'm struggling to get Technokinetics' code working. I can't post a link yet as the site is on a dev server. The code that I was using previously (with exclude_tree) prior to realizing it was only going to work for one post is/was:

    <li class="widget widget_text">
    <?php
    global $wp_query;
    $thePostID = $wp_query->post->ID;
    $children = wp_list_pages('exclude_tree=11&title_li=&child_of='.$thePostID.'&echo=0&depth=1');
    if ($children) { ?>

    <h3>More Information</h3>
    <ul id="widgettime"><?php echo $children; ?>
    <?php }
    ?>

    Please show me how Technokinetics code fits into the above (I've modified Technokinetics code to include my posts here):

    <?php $parent_pages_to_exclude = array(4,5,7,9,11);
    foreach($parent_pages_to_exclude as $parent_page_to_exclude) {
    if ($page_exclusions) { $page_exclusions .= ',' . $parent_page_to_exclude; }
    else { $page_exclusions = $parent_page_to_exclude; }
    $descendants = get_pages('child_of=' . $parent_page_to_exclude);
    foreach($descendants as $descendant) {
    $page_exclusions .= ',' . $descendant->ID;
    }
    }
    wp_list_pages('title_li=&sort_column=menu_order&exclude=' . $page_exclusions); ?>

    -------------
    Thanks!

  11. sknowlton
    Member
    Posted 2 years ago #

    for WP version 2.9.2
    i have read that this is fixed, but i dont see it.
    i reworked come core code, but don't know where to submit it.
    change line 2620+ of wp-includes/post.php to:

    if ( !empty($exclude_tree) ) {
    		$exclude_trees = preg_split('/[\s,]+/',$exclude_tree);
    		if ( count($exclude_trees) ) {
    			$num_pages = count($pages);
    			foreach ( $exclude_trees as $tree ) {
    				$exclude = (int) $tree;
    				$children = get_page_children($exclude, $pages);
    				$excludes = array();
    				foreach ( $children as $child )
    					$excludes[] = $child->ID;
    				$excludes[] = $exclude;
    				for ( $i = 0; $i < $num_pages; $i++ ) {
    					if ( in_array($pages[$i]->ID, $excludes) )
    						unset($pages[$i]);
    				}
    			}
    		}
    	}

Topic Closed

This topic has been closed to new replies.

About this Topic