Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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!

share|improve this question
    
Can you post your actual WP_Query() code? –  Chip Bennett Nov 21 '12 at 19:58
    
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 –  stevetronix Nov 21 '12 at 21:06
    
Can you please post that as an answer. Thanks! –  Chip Bennett Nov 21 '12 at 21:09
add comment

2 Answers

up vote 1 down vote accepted

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
share|improve this answer
    
Some code would be useful. –  toscho Nov 23 '12 at 17:48
add comment

If I understand what you want, when you register a CP on your theme, there is an option "exclude_from_search" set it to false and it won't show on search results.

For more information check: Register Post Type

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.