WordPress.org

Ready to get started?Download WordPress

Forums

Accessing array object keys (9 posts)

  1. hilmon
    Member
    Posted 2 years ago #

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

    This

    $totals=array(
    '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.

    :-p

  2. vtxyzzy
    Member
    Posted 2 years ago #

    I think this is what you want:

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

  3. hilmon
    Member
    Posted 2 years ago #

    Thanks, but I actually need those 2 array keys ...

    ['antique_silverware'][26]

    .. 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...

  4. Digital Raindrops
    Member
    Posted 2 years ago #

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

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

    or

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

    HTH

    David

  5. vtxyzzy
    Member
    Posted 2 years ago #

    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.

  6. hilmon
    Member
    Posted 2 years ago #

    @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...

  7. vtxyzzy
    Member
    Posted 2 years ago #

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

  8. hilmon
    Member
    Posted 2 years ago #

    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 http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/ plugin.. which populates the 'menu-order' key. Nice plugin by the way!

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

    http://www.dankerantiques.com/antique/silverware/irish-silver-embossed-circular-bowl/

    Thanks for all your help!

  9. hilmon
    Member
    Posted 2 years ago #

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

    http://oreilly.com/catalog/progphp/chapter/ch05.html

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.