Support » Fixing WordPress » Accessing array object keys

  • I’m trying to get the total number of posts in a specific taxonomy.


    'antique_silverware' => 'mugs',
    'post_type' => 'stock_silverware',
    'post_status' => 'publish',

    returns this

    Array ( [antique_silverware] => Array ( [26] => stdClass Object ( [term_id] => 26 [name] => Mugs [slug] => mugs [term_group] => 0 [term_taxonomy_id] => 130 [taxonomy] => antique_silverware [description] => [parent] => 0 [count] => 8 [object_id] => 688 ) ) [post_type] => stock_silverware [post_status] => publish )

    I need to access [count] => 8

    I totally stuck on how to access this…

    I’ve tried using get_object_vars($totals) but I presume it doesn’t work because the object is within an array?

    Many thanks in advance.


Viewing 8 replies - 1 through 8 (of 8 total)
  • I think this is what you want:

    $count = $totals['antique_silverware'][26]->count;

    Thanks, but I actually need those 2 array keys …


    .. to be dynamic as I’m using this in the single-loop.php template.

    I can set the first key ok but [26] I’m not so sure…

    It is a nested array, the array is zero based so untested code:

    $the_count = isset($totals[0][8]) ? $totals[0][8] : 0;


    $the_count = isset($totals['antique_silverware']['count']) ? $totals['antique_silverware']['count'] : 0;



    The term_id of Mugs is 26. The entry for $totals[‘antique_silverware’][26] is an object, so you use an object style reference to retrieve the count.

    @david that didn’t work, since the object is inside a nested array…. Damn, I’m really gonna have to learn more php :-/

    I’ve simplified the query by using…

    $terms = get_the_terms( $post->id, 'antique_silverware' );

    Which returns..

    Array ( [26] => stdClass Object ( [term_id] => 26 [name] => Mugs [slug] => mugs [term_group] => 0 [term_taxonomy_id] => 130 [taxonomy] => antique_silverware [description] => [parent] => 0 [count] => 8 [object_id] => 684 ) )

    So I just need to access the value of [count]…

    echo $terms[26]->count;

    This works but I need to find a way to reference [26] as a variable. This being (as vtzyzzy pointed out)… the term_id.

    Thanks for your help guys…. I’m sure this, like most things is simple enough, when you know how, but it has me totally stumped…

    What else do you know at the point of using get_the_terms()? Do you know the name of the term, or the slug?

    Got it working….

    Luckily each post only has a single term so…

    This is outside the loop as I’m using 2 different taxonomy templates

    $terms = get_the_terms( $post->id, 'antique_jewellery' );
    foreach ( $terms as $term ) { $id = $term->term_id; }
    $GLOBALS[ 'item-total' ] = $terms[$id]->count;

    This is inside the loop as I’m using single-loop.php for both taxonomies.

    <?php $item_num = $post->menu_order; $item_num++; ?>
    <p>Item <?php echo $item_num; ?>of <?php echo $GLOBALS['item-total']; ?></p>

    I’m using this in conjunction with the plugin.. which populates the ‘menu-order’ key. Nice plugin by the way!

    Here’s a link to see it all in action!

    Antique Irish Silver Embossed Circular Bowl

    Thanks for all your help!

    By the way came across this resource for php arrays…. study time again I think!!!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Accessing array object keys’ is closed to new replies.