0
votes

I want to be able to list ALL posts (children and parent) when browsing a parent category.

Like this:

  • Parent (Show posts from Child 1 and Child 2)
    • Child 1 (Show only posts from Child 1)
    • Child 2 (Show only posts from Child 2)

With this code below (placed in category.php) I don't get all the posts when I'm in a parent category. I just get posts from ONE child category instead of several.

Any ideas how to solve this?

<?php get_header(); ?>
    <div class="container">
        <div class="row">
            <?php get_template_part( 'include-cat-tag' ); ?>
            <div class="col-xs-12 col-sm-9 col-md-9 list-page-middle">
                <header class="clearfix">
                    <h1><?php single_cat_title( '', true ); ?></h1>
                </header>
                <?php
                    wp_reset_query();
                    $categories = get_the_category();
                    $category_id = $categories[0]->cat_ID;
                    $args = array(
                        'posts_per_page' => 100,
                        'category__in' => array($category_id),
                        'orderby' => 'meta_value title',
                        'order' => 'ASC',
                        'post_status' => 'publish',
                        'meta_key' => 'betyg',
                        'child_of' => $category_id
                    );
                    query_posts( $args );
                    if (have_posts()): while (have_posts()) : the_post();
                    get_template_part( 'include-list-post' );
                ?>
                <?php endwhile; ?>
                <?php else: ?>
                    <?php get_template_part( 'include-no-post' ); ?>
                <?php endif; ?>
            </div>       
        </div>
        <?php 
            get_template_part( 'include-list' );
            get_template_part( 'include-social' );
        ?>
        </div>
    </div>
    <?php get_footer(); ?>
1

1 Answers

0
votes

I think there is a few problems with your query.

  1. "child_of" is not supported for query_posts/get_posts function. Instead, it is only a valid option for get_categories. I think you are confused between querying the posts and categories here. You can check parse_query or get_posts function to see all options available.

https://codex.wordpress.org/Plugin_API/Action_Reference/parse_query https://developer.wordpress.org/reference/functions/get_posts/

  1. "meta_key" option shown but "meta_value" missing.

  2. It is also not recommended by WP to use query_posts directly but to use get_posts function or to hook to the pre_get_posts action.

To get the list of all posts of of child categories of a category, you can do something as follows:

<?php
$categories = get_categories(array("child_of" => $parent_category->term_id));

$posts_to_display = get_posts("category" => $parent_category->term_id);
     
foreach($categories as $category){
     // query child posts of this category here using get_posts
     // then merge them to the $posts_to_display array
}

//Do what ever you want with the array

Or if you still want to make use of query_posts and the Wordpress loop not using get_posts, use can hook to the pre_get_posts action:

<?php
function get_posts_for_parent_category( $query ) {
    // make sure that you are in the category page not single post 
    // or custom post type page
    if(!$query->is_category)
        return;
        
    $parent_category = $query->get_querried_object();
    $categories_to_query = array($parent_category->term_id);
    
    // Using get_categories to get all child categories_to_query
    // Get their ids and add it to the $categories_to_query array
    
    $query->set("category__in",$categories_to_query);
}
add_action( 'pre_get_posts', 'get_posts_for_parent_category' );