WordPress.org

Ready to get started?Download WordPress

Forums

show only grand children (10 posts)

  1. slee
    Member
    Posted 4 years ago #

    i am using something like this:

    $args=array(
    'post_type' => 'page',
    'post_parent' => 116,
    'meta_key' => $key,
    'meta_value' => $mvalue,
    'orderby' => $orderby,
    'posts_per_page' => 50,
    'order' => $order
    );
    query_posts($args);

    but is there a way i can only show the grandchildren ie children's children?

  2. MichaelH
    Member
    Posted 4 years ago #

    Use get_pages and something like this in your loop:

    if ($post->post_parent != 116 ) {
  3. slee
    Member
    Posted 4 years ago #

    i have tried this:

    $args=array(<br />
    		  'post_type' => 'page',<br />
    		  'orderby' => $orderby,<br />
    		  'posts_per_page' => 50,<br />
    		  'child_of' => 116,<br />
    		  'order' => $order<br />
    		);<br />
    		get_pages($args);

    with if ($post->post_parent != 116 ) {as you suggested in the loop,

    but that is only returning a link to the page that this is running on???

    can i use it with the normal loop or doi have to use a foreach loop?

  4. MichaelH
    Member
    Posted 4 years ago #

    You would need a loop of some kind to iterate through the array of pages returned by get_pages.

    Could use the same code I gave you in your other thread, but use this instead of the get_posts/query_posts

    $args=array(
    'post_type' => 'page',
    'post_parent' => 116,
    'meta_key' => $key,
    'meta_value' => $mvalue,
    'orderby' => $orderby
    );
    $pages = get_pages($args);
  5. slee
    Member
    Posted 4 years ago #

    i really appreciate your help but using that with the previous code doesn't output anything even before i put the if ($post->post_parent != 116 ) {

    i hav added two pages that are children of two different pages that have the parent of 116 so i should get 2 pages outputted.

    if i remove the meta_key, meta_value and the orderby then i get every single page.

  6. MichaelH
    Member
    Posted 4 years ago #

    Well I used this with success:

    <?php
    echo 'get pages';
    $parent = 93;
    $args=array(
      'child_of' => $parent
    );
    $pages = get_pages($args);
    if ($pages) {
      foreach($pages as $post) {
        setup_postdata($post);
        if ($post->post_parent != $parent ) { ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        }
      } // foreach($pages
    } // if ($pages
    ?>
  7. slee
    Member
    Posted 4 years ago #

    ah it was because i still had:

    'post_type' => 'page',

    which obviously isnt needed any more

    thanks for the help :)

  8. slee
    Member
    Posted 4 years ago #

    i have hit a problem with this, i want to be able to filter the results by using meta_key and meta_value but when i use the meta_value it does not work.

    i have simply got `'meta_key' => $key,
    'meta_value' => $mvalue,`

    which then using get capture the value but even if i hardcode them they don't work.
    i have a custom field called location and i have 1 page with the value Greece but if i use meta_value greece with meta_key location nothing is outputted am i doing something wrong?

  9. MichaelH
    Member
    Posted 4 years ago #

    This works okay for me:

    <?php
    echo 'get pages';
    $parent = 93;
    $args=array(
    'child_of' => $parent,
    'meta_key' => 'cf1',
    'meta_value' => 'cf1 value'
    );
    
    $pages = get_pages($args);
    if ($pages) {
      foreach($pages as $post) {
        setup_postdata($post); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
      } // foreach($pages
    } // if ($pages
    ?>
  10. slee
    Member
    Posted 4 years ago #

    i tried your code replacing he cf1 with location and greece for the value but nothing was outputted. when you tested it did you try it with the grandchildren?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.