1

So I'm trying to get a list of terms inside a taxonomy that is related to users. Inside each term (think a sub list) should be a list of the users for that term. Here is the code I've been working with. But it returns several errors for the query and non-object issues. Help?

<?php $disciplines = get_terms('disciplines');
  foreach($disciplines as $discipline) {
    echo '<li><a href="#">' . $discipline->name . '</a></li>';

    $post_args = array( 'post_type'  =>  'users', 'disciplines' => $discipline->term_id);
    $posts = get_posts( $post_args );

    foreach( $posts as $post ){
      echo 'post title: ' . $post->post_title . '<br />';
    }
  }

?> 
0

get_terms and get_posts return arrays not objects.

Try this:

<?php $disciplines = get_terms('disciplines');
  foreach($disciplines as $discipline) {
    echo '<li><a href="#">' . $discipline['name'] . '</a></li>';

    $post_args = array( 'post_type'  =>  'users', 'disciplines' => $discipline['term_id']);
    $posts = get_posts( $post_args );

    foreach( $posts as $post ){
      echo 'post title: ' . $post['post_title'] . '<br />';
    }
  }

?> 
  • Fatal error: Cannot use object of type stdClass as array in file on line 4. Specifically referring to $discipline['name'] . – mrpritchett Aug 18 '14 at 18:26
  • Then fix it! $discipline->name – zeros-and-ones Sep 20 '17 at 21:07

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.