---
title: "How to Show Related Post by Child Category in WordPress?"
url: https://nexterwp.com/docs/show-related-post-by-child-category-in-wordpress/
date: 2023-05-06
modified: 2026-04-14
author: "Aditya Sharma"
description: "Showing related posts by child category can help improve the user experience on your WordPress site by providing more relevant content to your visitors. By displaying related posts based on..."
image: https://nexterwp.com/wp-content/uploads/2024/05/show-related-post-by-child-category-in-wordpress-1024x519.jpg
word_count: 322
---

# How to Show Related Post by Child Category in WordPress?

## Key Takeaways

- Nexter Blocks Post Listing block is the block used to show related posts by child category in WordPress, and it requires Nexter Blocks to be installed and activated.
- WordPress does not provide a default option for related posts by child category, so the post uses custom code in a Child theme function.php file or plugins like Code Snippets or Nexter Extensions.
- tpgb_custom_doc_query is the filter key added in the Post Listing block’s Query tab Custom Query ID field after selecting the listing type, post type, style, and layout.

Showing related posts by child category can help improve the user experience on your WordPress site by providing more relevant content to your visitors. By displaying related posts based on the child category of the current post, you can keep your readers engaged and encourage them to explore more of your content.

To achieve this, you can use the Post Listing block from the Nexter Blocks.

*To check the complete feature overview documentation of the Nexter Blocks Post Listing block,* [click here](/docs/post-listing-block-settings-overview/).

***Requirement  - This block is a part of the Nexter Blocks, make sure its installed & activated to enjoy all its powers.***

[LIVE BLOCK LINK](https://nexterwp.com/nexter-blocks/listing/wordpress-post-listing/)

By default, in WordPress, there is no option to show related posts by a child category. For this, you have to add some custom code. You can add the code in your Child theme function.php file or you can use plugins like [Code Snippets](https://wordpress.org/plugins/code-snippets/) or [Nexter Extensions](https://nexterwp.com/nexter-extension//), which comes with various extensions, including adding code snippets.

> *Note: This feature is for advanced users only. If you are comfortable with PHP only, then you should use it.*

For example, we have added this code.

`add_filter('tpgb_custom_doc_query', 'tpgb_custom_doc_query_function',10,1);

function tpgb_custom_doc_query_function( $query_args ){

global $post;
$category_ids = [];
$categories = isset($post->ID) ? wp_get_post_terms($post->ID,'docs') : [];
foreach($categories as $category) {
if( 0 != $category->parent ){
$category_ids[] = $category->slug;
}

}

if(isset( $query_args['tax_query'] ) && !empty($query_args['tax_query'])){
foreach( $query_args['tax_query'] as $key => $argc) {
if($argc['taxonomy'] == 'docs' ){
$query_args['tax_query'][$key]['terms'] = $category_ids;

}
}
}
return $query_args;
}
`

Now to show related posts by child category based on this code, add the Post Listing block on the page or template, and select the appropriate listing type, post type, style and layout.

Then go to the **Query** tab and in the **Custom Query ID** field, add the filter key i.e. *tpgb_custom_doc_query* here. 

![](https://nexterwp.com/wp-content/uploads/2023/05/post-listing-custom-query-id-new.png)

Now it will show related posts by child category.

Also, check [How to Show Related Posts for Custom Post Type in WordPress](https://nexterwp.com/docs/show-related-posts-for-custom-post-type-in-wordpress/).