0

I currently have a piece of code I'm using to display a list of the three most recent 'special' custom post type posts which will display an image, title and content of these posts.

I'm trying to modify my code to show only third tier posts (get rid of parent and child pages and show only grandchildren).

The code I'm using is as follows:

<?php
                $query_args = array(
                    'post_type' => 'special',
                    'posts_per_page' => 3,
                    'order' => 'DESC',
                );
                $query = new WP_Query($query_args);
                while($query->have_posts()) : $query->the_post();
            ?>

As mentioned, this works perfectly to show the most recent posts, but I have been unable to get this to show only third tier.

For example, I've tried this:

'child_of' =>'.$post->ID.'

to no avail.

I've also tried implementing suggestions from a number of threads:

Best Practice For Querying Grandchildren?

Any help would be greatly appreciated.

Cheers,

Rob

EDIT -

I've made some headway. I've managed to remove top tier posts from displaying however second tier items still remain. My code is as follows:

In functions.php

 function wpse29897_no_parents( $where, $query )
 {
     if( isset( $query->query_vars['special'] ) && 'special' ==        $query->query_vars['post_type'] )
{
    if( '' != $where )
    {
        $where .= ' AND post_parent !=  0';
    }
    else
    {
        $where .= ' post_parent != 0';
    }
}
return $where;
 }

My query in the template:

 <?php
            add_filter( 'posts_where', 'wpse29897_no_parents', 10, 2 );
                $query_args = array(
                    'post_type' => 'special',
                    'posts_per_page' => 3,
                    'order' => 'DESC',
                );
                $query = new WP_Query($query_args);
                while($query->have_posts()) : $query->the_post();
            ?>
  • Is this for grandchildren of a particular post, or any 3rd-level special? – TheDeadMedic Apr 21 '15 at 12:58
  • Any 3-rd level special custom post type – Rob Apr 21 '15 at 13:17
  • This would be easy if WP_Query or get_pages supported 'depth' like wp_list_pages does – Rob Apr 23 '15 at 0:15
0

The only (easy) way this can be done is to query all special, calculate their depth, and then build a new stack of 3-level deep:

$grandkids = array();
$specials  = get_posts(
    array(
        'posts_per_page' => -1,
        'order' => 'DESC',
    )
);

foreach ( $specials as $special ) {
    if ( count( get_post_ancestors( $special ) ) === 2 ) {
        $grandkids[] = $special;

        if ( count( $grandkids ) === 3 )
            break;
    }
}

foreach ( $grandkids as $post ) {
    setup_postdata( $post );

    // Do your stuff
}
|improve this answer|||||
  • Actually there is an issue with this. I have this code in a sidebar in my custom post types 'singles' template. If I visit a post, the post is included up the top in the sidebar. Also 'posts_per_page' is set to 12 but only showing 3. I did however manage to find a solution to my problem. All my third level posts share a unique custom field that is not being used by a top or second level post. Therefore, I've added 'meta_key' => 'key-name' to my args. – Rob Apr 29 '15 at 4:23
  • Nice one. Although worth mentioning, you need posts_per_page => -1 initially to get all posts, in order to calculate which ones are three levels deep. Any limits should be placed in the subsequent foreach loop. – TheDeadMedic Apr 29 '15 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.