Get posts from child categories with parent category ID

I have a plugin that shows posts from a chosen category.
When child categories are chosen it works perfectly.
However when i choose a parent category, it doesn’t display any posts from the child categories.

I hope this is the snippet that has to be changed, but i’m not sure.

Would be nice if someone could help me.

<ul>

<?php 
   query_posts(array(
      'post_type' => 'post', 
      'category__in' => $text, 
      'orderby' => 'meta_value_num', 
      'meta_key' => 'rankk', 
      'order' => 'DESC', 
      'posts_per_page' => 100
   )); 
?>

Solutions Collecting From Web of "Get posts from child categories with parent category ID"

As I have stated in my comments to your question

Crappy written plugins always leads to some disaster at some time. In my opinion, delete that plugin and write your own code or find a properly written plugin. There is no use changing the damaged tire while the whole car is a complete write-off 🙂

Just to start off, never use query_posts

Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

You are really better off here to write your own code and discard the plugin. You can also merge the current plugin (only the “good” code) with the code I will give you into your own plugin if this is needed.

You should never ever make any changes to plugin/theme files that you are not the author of. The biggest reason is, come update day, you will loose all you customization. Rather create your own plugin or do the changes in a child theme

What you want to accomplish is not doable with the current category parameters. You best bet here will be to use a tax_query with WP_Query. With a tax_query, by default, child terms are included to the term being set, and this is what you are looking for

You can try the following (PLEASE NOTE: this requires PHP 5.4+)

$args = [
      'post_type' => 'post', 
      'orderby' => 'meta_value_num', 
      'meta_key' => 'rankk', 
      'order' => 'DESC', 
      'posts_per_page' => 100,
      'tax_query' => [
            [
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => 'YOUR PARENT CATEGORY ID',
            ],
        ],
];

$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
        the_title();
    }
    wp_reset_postdata();
} 

For PHP versions prior to 5.4 try this

$args = array(
      'post_type' => 'post', 
      'orderby' => 'meta_value_num', 
      'meta_key' => 'rankk', 
      'order' => 'DESC', 
      'posts_per_page' => 100,
      'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => 'YOUR PARENT CATEGORY ID',
            ),
        ),
);

$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
        the_title();
    }
    wp_reset_postdata();
} 

you may use this way:

    <?php
$category_id = get_cat_ID('Category Name');
 <!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <!-- Test if the current post is in category "Category Name". -->
 <!-- If it is, the div box is given the CSS class "post-cat-special". -->
 <!-- Otherwise, the div box is given the CSS class "post". -->

 <?php if ( in_category($category_id) ) { ?>
           <div class="post-cat-special">
 <?php } else { ?>
           <div class="post">
 <?php } ?>
</div>
 <!-- Stop The Loop (but note the "else:" - see next line). -->

 <?php endwhile; else: ?>


 <!-- The very first "if" tested to see if there were any Posts to -->
 <!-- display.  This "else" part tells what do if there weren't any. -->
 <p>Sorry, no posts matched your criteria.</p>


 <!-- REALLY stop The Loop. -->
 <?php endif; ?>

use this magic code wherever you need to display….