---
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 includes the Post Listing block, which allows users to display related posts by child category.
- WordPress does not provide a default option for showing related posts by child category, requiring custom code or plugins like Nexter Extensions.
- Users must add a custom query filter key in the Query tab of the Post Listing block to show related posts by child category.

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/).

## Frequently Asked Questions

**Q: What should I do if the related posts by child category are not showing up?**
A: If related posts by child category are not displaying, ensure that the Post Listing block from Nexter Blocks is installed and activated. Additionally, verify that you've added the correct custom query ID in the Query tab of the block settings. Without this setup, WordPress won't know to filter posts by child category.

**Q: What is the best practice for implementing the custom code for related posts?**
A: When implementing the custom code for related posts, it's advisable to add it to your Child theme's functions.php file. This approach ensures that your changes are preserved during theme updates. If you're not comfortable with PHP, consider using plugins like Code Snippets or Nexter Extensions, which simplify the process of adding custom code.

**Q: Does NexterWP provide any built-in options for showing related posts by child category?**
A: NexterWP does not offer built-in options for showing related posts by child category out of the box. You need to add custom code to achieve this functionality. The Post Listing block from Nexter Blocks is essential for displaying these related posts effectively.

**Q: Are there any limitations when showing related posts by child category?**
A: One limitation when showing related posts by child category is that it requires some familiarity with PHP for custom coding. This feature is primarily aimed at advanced users. If you're not comfortable with coding, using Nexter Extensions can help simplify the process by allowing you to add snippets without directly editing theme files.
