Forums

[resolved] Exclude duplicated posts (4 posts)

  1. simonebiffi
    Member
    Posted 2 years ago #

    Hi there,

    I am trying to find a way to allow posts listings (and related images) to identify which post is being loaded and hence exclude it from being duplicated on the same page. This way if you have a thumbs listing of your most recent articles, let’s say on the sidebar, you exclude the one that you are currently reading to prevent it from being redundant on the page.

    In my functions.php I have the following code:

    $included_pic = array();
    
    $pic_childs = get_categories('child_of='.$chic['category_2_id']);
    
    foreach($pic_childs as $pic_child) {
    array_push($included_pic, $pic_child->cat_ID);
    }
    
    array_push($excluded, $chic['category_2_id']);
    array_push($included_pic, $chic['category_2_id']);

    where $chic['category_2_id'] simply retrieves the category ID already provided in a config.inc file.

    On the post file I then include this to load the article, making sure it is identified and hence excluded from being duplicated elsewhere on the same page:

    <?php global $included_pic; $showpost = new WP_query(); $showpost->query(array('category__in'=>$included_pic,'post__not_in'=>array($post->ID),'showposts'=>3)); ?>
    <?php if ($showpost->have_posts()) : ?>
    <?php while ($showpost->have_posts()) : $showpost->the_post();  ?>

    The problem here is that such function only allows me to target the precise category_2 in a situation where there would be plenty more categories to apply this to. If I were to replicate the same code for each of the categories, I would find myself with a huge replicated function, simply unnecessary and extremely heavy to load.

    Would someone know how to streamline the above code in order for the function to identify immediately at loading in which category I am each time a new post is loaded, and hence automatically get the category ID?

    Hope someone can help.
    Thanks in advance.
    S.

  2. monkeymynd
    Member
    Posted 2 years ago #

    Look here and search on $do_not_duplicate.

    http://codex.wordpress.org/The_Loop

  3. simonebiffi
    Member
    Posted 2 years ago #

    That worked just PERFECTLY!! Sweet and easy!

    Thanks, Monkeymynd, much appreciated.

  4. monkeymynd
    Member
    Posted 2 years ago #

    Glad you got it worked out :)

Topic Closed

This topic has been closed to new replies.

About this Topic