• I’ve written a custom database query t display posts by meta value. I’m having an issue though – some of my posts are in more than one category, and because of this, it’s displaying the post once for each category. I only want it to display once.

    For example, if the post is in “books” and “horror”, then the post shows up twice on the page.

    Would anyone know how to get it to show up only once? I’ve tried “if(in_category())” to change it – but because it’s in the category I *don’t* want to show, the category I *do* want to show also disappears because it’s in the other category, as well. I’m sure it has to do with the “foreach” statement, but I’m not sure how to flag it so it skips the one category, but displays the other.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Maybe there’s a more elegant way, but here’s a hack that will do it.

    Before your loop starts:

    $usedPosts = array(); // necessary, else in_array() fails

    At the beginning of your loop:

    if (in_array( get_permalink, $usedPosts ))
      continue;
    
    $usedPosts[] = get_permalink();
    Thread Starter Doodlebee

    (@doodlebee)

    thanks Adam 🙂 I’ll give that a shot.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I’ve written a custom database query…some of my posts are in more than one category, and because of this, it’s displaying the post once for each category.

    Why not change your query such that it only has each post once? GROUP BY post_id, or some such thing. Maybe add a DISTINCT in the right place, perhaps?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘skip *and* show a post?’ is closed to new replies.