Skip to content

Instantly share code, notes, and snippets.

@brichards
Last active December 24, 2015 01:09
Show Gist options
  • Save brichards/6722068 to your computer and use it in GitHub Desktop.
Save brichards/6722068 to your computer and use it in GitHub Desktop.
Returns an array of queried results. Behaves as expected, with GROUP BY limiting my results to a single entry for each term_id, whereas no GROUP BY returns a separate entry for each term_id differing object_id. Desired outcome: single entry per term_id which contains all connected object_ids. Current output: single entry per term_id (success!). …
<?php
$terms = $wpdb->get_results(
"
SELECT term.term_id,
term.name,
term.slug,
taxonomy.term_taxonomy_id,
relationship.object_id
FROM $wpdb->terms as term
INNER JOIN $wpdb->term_taxonomy as taxonomy
ON taxonomy.term_id = term.term_id
INNER JOIN $wpdb->term_relationships as relationship
ON relationship.term_taxonomy_id = taxonomy.term_taxonomy_id
WHERE taxonomy.taxonomy = 'branch'
GROUP BY term.term_id
"
);
// print_r( $terms )
Array(
[0] => stdClass Object
(
[name] => Bedford-Stuyvesant YMCA
[slug] => 40
[term_id] => 16
[term_taxonomy_id] => 19
[object_id] => 925
)
[1] => stdClass Object
(
[name] => Bronx YMCA
[slug] => 2
[term_id] => 39
[term_taxonomy_id] => 42
[object_id] => 925
)
)
// Desired Output
Array(
[0] => stdClass Object
(
[name] => Bedford-Stuyvesant YMCA
[slug] => 40
[term_id] => 16
[term_taxonomy_id] => 19
[object_id] => array(
925,
926,
927,
930
)
)
[1] => stdClass Object
(
[name] => Bronx YMCA
[slug] => 2
[term_id] => 39
[term_taxonomy_id] => 42
[object_id] => array(
932,
934,
935
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment