1

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!

  • 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
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
  • Some code would be useful. – fuxia Nov 23 '12 at 17:48
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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