• Resolved moacirsantana

    (@moacirsantana)


    Hello, I created a custom single page that’s working perfectly and now I’ve decided to display related posts with a custom loop:

    $args = array(
    'post_type' => 'advert',
    'posts_per_page' => 8,
    'menu_order' => 1,
    'orderby' => 'rand',
    'order'    => 'ASC');

    So as you can see they are random featured posts, but they must be in the same category or categories of the current ad. What arguments should I add?

    Thank you

    • This topic was modified 5 years, 8 months ago by moacirsantana.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    are you using the Related Ads snippet (https://github.com/simpliko/wpadverts-snippets/blob/master/related-ads/related-ads.php) to show the related Ads?

    If so then in order to show related Ads by category you would need to change the line

    
    $args["s"] = $post->post_title;
    

    to

    
    $terms = wp_get_post_terms( $post->ID, 'advert_category' );
    $terms_list = array();
    foreach( $terms as $term ) {
        $terms_list[] = $term->term_id;
    }
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'advert_category',
            'field'    => 'term_id',
            'terms'    => $terms_list,
        ),
    );
    
    Thread Starter moacirsantana

    (@moacirsantana)

    No, I have been using a completely different single page created from scratch, but those arguments were what I needed to get it working in my custom loop. Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Display Related Posts by Categories’ is closed to new replies.