Need to convert string of term ids into comma separated integers for use in an arguments array

So I’m working on a medical site that has a custom post type “doctors”. Within this post type are custom taxonomies for “locations” and “procedures”. I’ve created a custom taxonomy-locations.php file to control how my locations pages look, on this page is some general information like contact info, maps, related doctors. I also need to include a list of all procedures offered at each location. Since procedures are attached to each doctor, not to each location, I created a function containing a loop to run through all doctors tagged at the current location and output a comma separated list of all tagged procedures term ids. See below:

// Get the doctors procedures
function location_doctors_procedure_loop() {

$tax_slug = get_query_var( ‘locations’ );

$args = array(
‘posts_per_page’ => -1,
‘order’ => ‘DESC’,
‘post_type’ => ‘our_team’,
‘locations’ => $tax_slug

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

while ( $the_query->have_posts() ) {


$terms = get_the_terms( $post, ‘procedures’ );

if ( !empty($terms) ) {
foreach( $terms as $term ) {
echo $term->term_id . ‘,’;





I then created a function that loops through all active procedure taxonomies and lists them hierarchically as parent, child, grandparent. See below:

// Get the procedures
function tax_location_procedures() {

$doctor_procedures = location_doctors_procedure_loop();

$terms = get_terms( array(
‘taxonomy’ => ‘procedures’,
‘hide_empty’ => true,
‘include’ => array( $doctor_procedures ),
) );

echo ‘<h2 class=”doctor-bio-procedure-condition-header”>Procedures</h2>’;

if ( !empty($terms) ) {
echo ‘<div class=”doctor-bio-procedures”>’;
foreach( $terms as $term ) {
if( $term->parent == 0 ) {
<p class=”doctor-bio-procedure-condition-sub-header”><?php echo $term->name; ?></p>
echo ‘<ul>’;
foreach( $terms as $childterm ) {
if($childterm->parent == $term->term_id) {
echo ‘<li>’ . $childterm->name . ‘</li>’;
echo ‘<ul>’;
foreach( $terms as $grandchildterm ) {
if($grandchildterm->parent == $childterm->term_id) {
echo ‘<li>’ . $grandchildterm->name . ‘</li>’;
echo ‘</ul>’;
echo ‘</ul>’;
echo ‘</div>’;


What I’m trying to do is use my first function “location_doctors_procedure_loop()” to populated the ‘include’ arguments array in “function tax_location_procedures()”. The problem I’m having is that while “location_doctors_procedure_loop()” does echo out a comma separated list of the correct term IDs (e.g 231,229,) it does nothing for my ‘include” argument because its echoing as a string rather than integers, so it’s reading as

‘include’ => array(‘231,229,’)

rather than

‘include’ => array(231,229,)

I’m stuck on this and have already spent the better part of a day trying to get this to work properly. Any help you guys can provide would be greatly appreciated.

Read more here:: Need to convert string of term ids into comma separated integers for use in an arguments array

Leave a Reply

Your email address will not be published. Required fields are marked *