Support » Themes and Templates » Excluding child categories, but not their grandchildren

  • Resolved lukeshumard

    (@lukeshumard)


    I’m trying to list the grandchild pages of a page, but not it’s direct children. In this example, the page whose grandchildren I am trying to get is “Portfolio”. There are three different pages within Portfolio (for different types of media), and then about 3 within each of those (portfolio items to show).

    I want all of the portfolio items to show at once, but I don’t want their parent pages (the types of media) to show up in there as if they were portfolio items themselves. I’ve tried to set a custom value to the pages I want to show up, but these don’t show up unless their respective parent page also has the same custom value.

    Is there anyway to exclude the child pages, but not the grandchild pages within? Anyways, here is my code. Any help is appreciated.

    <?php $parentID = 20;
    	$args = array(
    		   'child_of' => $parentID,
    		   'meta_key' => "splash",
    		   'meta_value' => "true"
    		   );
    	$pages = get_pages($args);
    		foreach($pages as $child) {
    			$portname = get_the_title($child->ID);
    			$portlink = get_permalink($child->ID); ?>
                <h3><a href="<?php echo $portlink; ?>"><?php echo $portname; ?> / <span class="category"><?php echo $portcat; ?></span> </a></h3>
                <?php } ?>

Viewing 12 replies - 1 through 12 (of 12 total)
  • MichaelH

    (@michaelh)

    Couldn’t you simply do this:

    foreach($pages as $child) {
      if ($child->post_parent != $parentID) {
        $portname = get_the_title($child->ID);
        $portlink = get_permalink($child->ID);
      }  ?>
    Thread Starter lukeshumard

    (@lukeshumard)

    no, because this is on the index. when i input that, it just results with the first query, but that’s it. i don’t know why it isn’t going for the rest.

    edit: actually, i don’t think it matters that this is on the index, but oh well.

    Thread Starter lukeshumard

    (@lukeshumard)

    got it to work! michaelh’s code actually worked for me, i just had to play with my theme a bit. thank you again for the help.

    Thread Starter lukeshumard

    (@lukeshumard)

    okay, not totally resolved. it turns out the results are sorted by their parent pages. i have it set in “get_pages” to have them sorted by creation date, but they’re only sorted this way within their child categories. any suggestions on how to get past this?

    MichaelH

    (@michaelh)

    Look at the various sort_column arguments in Function_Reference/get_pages.

    Thread Starter lukeshumard

    (@lukeshumard)

    i’ve looked at the sort_column and the sort_order, and i already have them specified in my code. the portfolio items are still showing up organized by their parent page. i’ve also looked at wp_posts in the database description, but can’t figure out how i could use that to my advantage. here’s what i have now.

    <?php $parentID = 20;
    	$pages = get_pages("child_of=$parentID&sort_column=post_date&sort_order=desc");
    	foreach($pages as $child) {
    		if ($child->post_parent != $parentID) {
    			$portname = get_the_title($child->ID);
    			$portlink = get_permalink($child->ID);
    			$portcat = get_the_title($child->post_parent);
    			$thumbURL = get_post_meta($child->ID, 'portfolio-splash', true); ?>
    		<div class="portfolioitem">
                <a href="<?php echo $portlink; ?>" class="portfoliolink"></a>
                <h3><?php echo $portname; ?> / <span class="category"><?php echo $portcat; ?></span></h3>
                <img src="<?php echo $thumbURL; ?>" alt="" />
            </div>
                <?php } } ?>

    MichaelH

    (@michaelh)

    So everything is working correctly but the order that that pages are displayed?

    Guess you better put your whole template at wordpress.pastebin.com and report the link back here.

    Thread Starter lukeshumard

    (@lukeshumard)

    that is the whole page template, pretty much. for the sake of doing it, though, here it is.
    http://pastebin.com/LJL9ixD4

    thank you so much for your help. this is doing my head in.

    MichaelH

    (@michaelh)

    Okay so I understand it correctly, you are trying to get all the grandchild (or even great grandchild) pages of page id 20 that have the custom field “splash” set to “true”?

    Thread Starter lukeshumard

    (@lukeshumard)

    that’s it, except i got rid of using the custom field “splash” set to “true”. now it just needs to get all grandchild pages of page id 20 and sort them from newest to oldest by date. the only problem i’m having now is that the grandchild pages are all grouped together, regardless of creation date. here’s an image of what it’s looking like.
    http://i20.photobucket.com/albums/b219/lukeshumard/wp-grandchild-sorting.png

    blackcity records should be at the top, but all packaging show up first.

    MichaelH

    (@michaelh)

    I guess get_pages enforces the hierarchy that “can’t be broken”.

    So use something like this (you’ll have to put your fit your div’s and other stuff in:

    <?php
    $parentID = 20;
    $args=array(
      'child_of' => $parentID
    );
    $pages = get_pages($args);
    if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
    
      $args=array(
        'post__in' => $pageids,
        'orderby ' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'page',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List Generations of Page '.$parentID.' except for direct childern';
        while ($my_query->have_posts()) : $my_query->the_post();
          if ($my_query->post->post_parent != $parentID) {
     ?>
            <p><?php the_time('m.d.y') ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          }
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
    Thread Starter lukeshumard

    (@lukeshumard)

    thank you so much! this works perfectly. just so i’m clear on the logic of this…
    the hierarchy that get_pages queries pages from can’t be changed, which is why i couldn’t change their order outside of their parent page. to work around it, we use the results that get_pages gives us and create a separate query from that, in which it specifies exactly how we want the data to be organized.

    …right?

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Excluding child categories, but not their grandchildren’ is closed to new replies.