1

I am using the following code to display the related posts but it is showing posts for that category only like brand showing brand posts only. What I am looking for is if I select any of the posts under child (brand, advertising, online etc), it should display all posts under marketing so I will not have to assign multiple categories to the posts

  • Subject
    • Marketing(3)
      • Brand(1)
      • Advertising(1)
      • Online(1)
<?php

$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<ul> 
    <li>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        <?php the_content('Read the rest of this entry &raquo;'); ?>
    </li>
</ul>   

<?php } wp_reset_postdata(); ?>
0

Just change this one line:

   $cat_obj = $wp_query->get_queried_object();
    $thiscat_id = $cat_obj->term_id;
    $thiscat = get_category($thiscat_id);
    $chi = array();
    if (!empty($thiscat->parent)) {
        $parentcat = get_category($thiscat->parent);
        $categories_chi=get_categories(
            array( 'parent' => $parentcat->cat_ID )
        );

        foreach ($categories_chi as $key => $value) {
            $chi[] = $value->cat_ID;
        }
        $pare = array($parentcat->term_id);
        $ids = array_merge($pare, $chi);
    } else {
        $ids = array($cat_obj->term_id);
    }

    $related  = get_posts(array( 'category__in' => $ids, 'numberposts' => 10));
  • Thanks Sonali! I appreciate your willingness to help. Unfortunately, it's not working. Though I have replaced the $related line with your code. – stack kid Feb 24 '17 at 10:09
  • if you select brand then it should show marketing and brand both posts right? – Sonali Feb 24 '17 at 10:13
  • Yes, it should show all the posts available in marketing. In this example, you can consider one post for each child. So in marketing, we have 3 posts distributed in 3 children (brand, advert, online). In a nutshell, if I select any child element, it should select all the posts available in it's parent. For example, If I select brand, it should show the posts in advert, and online as related posts because they have common parent Marketing. Currently, it's empty. I am not assigning the direct category of marketing to each of them. Like, choosing the marketing, and brand categories for one post. – stack kid Feb 24 '17 at 10:27
  • You should try updated code.I have parent category and two child category ,if i open any child category it shows parent category posts as well as another child category posts.Hope this may help you – Sonali Feb 24 '17 at 10:40
  • by the way, if I assign both categories like marketing, and brand, then my code was showing the related posts. I am just trying to get rid of this parent category within posts..and keep it as parent only. – stack kid Feb 24 '17 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.