• Resolved MarketMeNot

    (@marketmenot)


    Hello,

    I’m trying to have 3 post links show up that are related to the current posts by a custom taxonomy. The links show up just fine, but the loop won’t exclude the current post from the list. Any gurus out there know what I’m doing wrong?? I’ve been at this for hours, and can’t seem to figure it out. It’s probably so simple that I’m overlooking it. Or I suck, either one.

    Here’s the code.


    $TaxName = 'ad-agency';

    if (has_term( '', $TaxName, $post->ID )) {

    $this_post = $post->ID;

    foreach((get_the_terms($post->ID, $TaxName)) as $Term) {

    $posts = new WP_Query( 'taxonomy='.$TaxName.'&term='.$Term->slug.'&posts_per_page=3&orderby=rand&exclude=' . $this_post );

    if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();

    //the content

    endwhile; endif;

    wp_reset_postdata();

    }

    }

    I have the code live and can provide a link if that helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter MarketMeNot

    (@marketmenot)

    Oooh ok, I think I figured it out. I had to array the taxonomy stuff into tax_query separate from the other stuff.

    'tax_query' => array(
    'relation' => 'AND',
    array(
    'taxonomy' => $TaxName,
    'terms' => $Term
    ),
    ),
    'post__not_in' => array($this_post),
    'post_status' => 'publish',
    'posts_per_page' => 3,
    'order' => 'DESC'

    That did it for me.

    Thread Starter MarketMeNot

    (@marketmenot)

    Yea seems good to me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Loop On Post Page Not Excluding Current Post’ is closed to new replies.