12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Nov 30, 2020

How to show x number of posts from each taxonomy term in WordPress

Sridhar Katakam

By default the output of a WordPress query is to show a specific number of or all posts that belong to all the categories (or custom taxonomies). What if you want to output say only a maximum of 3 portfolio CPT items from each portfolio_category?

For this one approach is to:

  • get all the terms of the taxonomy in question
  • loop through them and for each term, get the IDs of recent 3 items into an array
  • flatten the array
  • remove duplicates
  • run a WP_Query looping these IDs and output the items

Here's the PHP code:

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right