WordPress.org

Ready to get started?Download WordPress

Forums

Duplicate post issue on multiple category loops on one page (2 posts)

  1. andrewtoohill
    Member
    Posted 1 year ago #

    I am building a homepage template and it has an area with 4 separate category loops in a grid layout. Each loop will only show one post. The posts that I will be working with have multiple categories assigned to them and we don't want to have more than one instance of a post displaying amongst the 4 loops. For example if my latest post is assigned to all of the 4 categories that are setup to loop on the homepage, how can we set a priority so that it only displays in one of the categories and not the other three? I tried excluding the 3 other categories inside of each of the loops but then it just skips over all posts until it finds one that only has one category assigned to it and that won't work for this project. Let me know if you need further clarification.

    Does anyone have any thoughts or ideas on this.

    Thanks,

    Andrew

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I believe this will work:

    Inside each of the first 3 while() loops, add the ID to an array:

    while (have_posts()) : the_post();
       $do_not_duplicate[] = $post->ID;
       // rest of loop
    endwhile;

    In the last 3 queries, add the 'post__not_in' => $do_not_duplicate, argument to the query.

Topic Closed

This topic has been closed to new replies.

About this Topic