Query child category posts

So I’m trying to query posts that are in a child category name Website Development Portfolio. I can query posts in the parent category just fine like so. If I enter ‘Website Development Portfolio’ instead of ‘Portfolio’ then the query returns all posts. How can I return only the posts in the child category?

			<?php 
    
					$posts = get_posts(array(
						'posts_per_page'	=> 50,
						'post_type'			=> 'post',
						'child_of'			=> '17',
						'orderby'			=> 'name',
						'order'				=> 'ASC',
						'category_name'		=> 'Portfolio'
					));
					
					
					if( $posts ): ?>
						
						<ul id="HomePlansListUl" class="col-xs-12">
							
						<?php foreach( $posts as $post ): 
							
							setup_postdata( $post )
							
							?>
							<li class="col-lg-4 col-md-4 col-sm-6 col-xs-12 list-unstyled">
								<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
									<img class="" src="<?php the_field('home_image'); ?>" alt="" />
								</a>
							</li>
						
						<?php endforeach; ?>
						
						</ul>
                        
                        <? wp_reset_postdata(); ?>
                                
            <?php endif; ?>

When I look at the codex I don’t see an option for child categories.
https://codex.wordpress.org/Template_Tags/get_posts

Reading your WP Reference link:

Can it be that using a category name with spaces interprets as “Website+Development+Portfolio”?
Have you also tried to use the “category id” for the child category ‘Website Development Portfolio’?

Have you tried using the tag for that category:
“Display posts that have this tag, using tag slug”:

$query = new WP_Query( array( 'tag' => 'cooking' ) );

Or Simple Taxonomy Query:
“Display posts tagged with bob, under people custom taxonomy”:

$args = array(
	'post_type' => 'post',
	'tax_query' => array(
		array(
			'taxonomy' => 'people',
			'field'    => 'slug',
			'terms'    => 'bob',
		),
	),
);
$query = new WP_Query( $args );

You have probably tried that already, just my two cents.

This is the solution that I found works


            <?php //Display posts that are in the child category Website Development Portfolio ?>
            <?php $my_query = new WP_Query( array( 'posts_per_page' => 10, 'category__and' => array( 18 ) ) ); ?>
			<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
            
                <section id="" class="col-lg-4 col-md-4 col-sm-6 col-xs-12">
                    <h2><?php the_title(); ?></h2>
                    
                    <div class="excerpt-content">
                    <?php the_excerpt(); ?>
                    </div> 
                       
                    <?php $PreviewImage = get_field('design_image_preview');  ?>
                    <img class="" src="<?php echo $PreviewImage['url'] ?>" alt="<?php echo $PreviewImage['alt'] ?>"/>
                    
                </section>
            <?php endwhile; ?>

Nice!

(Not so skilled in WP, but I guess the category ID was “18”?)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.