0

A client of mine wants to create a simple "related posts" loop that shows 6 of the most recent posts that match the current post's category. The issue is that every one of their posts have the same parent category and then are organised using children of that category so simply using get_the_category as I have been doing isn't going to work as they all share the same parent.

So, my question is, is there a way that I can get the ID of the current post's lowest tier child category, but ignore it's parent(s)?

So for example if you're viewing a post that has the following category structure;

- Parent Category
    - First Child Category
        - Second Child Category

How could I get the ID of JUST the Second Child Category while ignorning it's parents?

1

Display every grandchildren on wordpress support forum seems pretty close.

Here is the solution that was proposed:

<ul>
<?php $all = get_pages();   //You could use $args to retrieve posts how you want.
foreach($all as $all) { 
 if($all->post_parent) { 
  if( get_page($all->post_parent)->post_parent ) { 
   $tp = get_page($all->post_parent);
   if( !get_page( $tp->post_parent )->post_parent) { $grandchild_ids[] = $all->ID; }    //catching all the ids of grandchild or secondchild.
  }
 }
}

//Usual args parameter & WP_Query loop
$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'orderby' => 'rand',
    'post__in' => $grandchild_ids
  );
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { 
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;
} 
wp_reset_query(); ?>
</ul>

In summary you would get 5 random posts from the list of grandchildren of all the parents.

0

Although @sri answer helps, here is a function that will get the deepest category even if depth is greater than 2. (Taken and modified from this answer.). This function uses wordpress's function get_ancestors().

function get_deepest_term($post_id){

    $the_terms = get_the_terms( $post_id, 'Category' );

    if ( $the_terms && ! is_wp_error( $the_terms ) ) {

        $deepest = $the_terms[0];
        $depth = 0;
        $term_depth= 0;

       // Find the deepest term
       foreach( $the_terms as $term ) {
           $term_ancestors = get_ancestors( $term->term_id, 'Category' );
           if ( $term_ancestors ) {
               $term_depth = count( $term_ancestors );
           }
           else $term_depth= 0;

           if ( $term_depth > $depth ) {
               $depth = $term_depth;
               $deepest = $term;
           }

       } // END: foreach( $the_terms as $term ) 

   }  // END: if ( $the_terms && ! is_wp_error( $the_terms ) ) 

    return $deepest;
}   // END: function get_deepest_term($post_id)

Then the above function can be used to loop through posts from deepest category as follows (Code taken and adjusted from @sri answer):

<?php

//Usual args parameter & WP_Query loop
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'orderby' => 'rand',
    'post__in' => array(get_deepest_term( get_the_ID() )->term_id)
);

$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
   <ul>

       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
       <?php endwhile;
      wp_reset_post_data();

} // END: if( $my_query->have_posts() ) ?>
   </ul>

So, my question is, is there a way that I can get the ID of the current post's lowest tier child category, but ignore it's parent(s)?

   $lowest_tier_child_category = get_deepest_term( get_the_ID() )->term_id

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.