Exclude pages in archives results

So, i got a custom post type applied to different posts and also a page. When i display this page, it shows some content (text), and at the bottom a link to get the list of the products related to this page (thanks to the CPT also applied to the page).

The link is called like this:

Read More
echo get_the_term_list( $post->ID, 'prod-cate', 'Products of the cateogry: ', ', ', '.' ); 

When i hit this link, it displays an archives page with the products but also the page i’m comming from.

The question is, how not to display this page in the archives results?

Related posts

Leave a Reply

1 comment

  1. Call the link with a query argument in which you pass the current post ID.

    If get_the_term_list returns a url address (the link href) you would need to append a argument to it. Then, on the archive page exclude that post ID before the loop, using something like:

    if(isset($_GET['related_to']))
      query_posts(array('post__not_in' => array(intval($_GET['related_to']))));
    

    Update: I’ve looked up get_the_term_list and it seems to be a wp function that lists html-formatted term links. So you’ll need to append your query argument before the links are built. In your functions.php file add:

    function my_related_link($termlink, $term, $taxonomy){
      global $post;
      return add_query_arg('related_to', $post->ID, $termlink);
    }
    

    and in the template file where the link is added:

    add_filter('term_link', 'my_related_link', 10, 3);
    echo get_the_term_list( $post->ID, 'prod-cate', 'Products of the cateogry: ', ', ', '.' );
    remove_filter('term_link', 'my_related_link');