Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?

Is it possible to remove custom post types from WP_QUERY that have a same shared custom taxonomy so that only one custom post type associated to that custom taxonomy is returned?

Example: Get an Items custom post type, returning only one Item per a custom taxonomy.

Read More

On a taxonomy-types.php page, I’m using WP_QUERY to get the Items custom post type. Then, wp_get_object_terms to get the Models custom taxonomy for the Items, and displayed that. However, I can end up with duplicate Models displayed because WP_QUERY is returning all Items and not one Item per unique Model.

Thank you for your help!

Related posts

Leave a Reply

2 comments

  1. Thanks guys. I think I may have found a solution.

    What I did:

    • On taxonomy-types.php, get all Items for that type using WP_QUERY.
    • Loop through them, use get_the_terms() to get taxonomy Model for each Item
    • Still in the loop: use PHP’s in_array() function and a $model_ids array for unique Model checking
    • Still in the loop: if the Model is unique, store the Item id in an $item_ids array
    • End that loop
    • Use WP_QUERY to get Items with 'post__in' set to the $item_ids array