• groq

    (@groq)


    Hi Guys,

    I’m working on trying to the list of id’s and have them put inside of an array. I’m currently using ‘get_the_id’ function inside a foreach loop. Then outside the loop, I’m trying to echo the largest id in the array. The issue I’m having is that the script returns the smallest id. My code is below, is there something I’m doing wrong?

    -groq

    <?php $posts = get_posts( "category=3&amp;numberposts=100" ); ?>
    <?php if( $posts ) : ?>
    <?php foreach( $posts as $post ) : the_post(); ?>
    
    <? $latestpost = array(get_the_id()); ?>
    
    <?php endforeach; ?>
    <?php endif; ?>
    
    <? echo max($latestpost); ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • MichaelH

    (@michaelh)

    Or this?

    <?php
    $args = array(
      'post_type' => 'post',
      'showposts' => '1',
      'orderby' => 'ID',
      'order' => 'DESC'
      );
    $posts=get_posts($args);
    if ($posts) {
    foreach($posts as $post) {
    echo 'highest post id '. $post->ID;?>
    }
    }
    ?>

    michael,

    If I’m trying to make an array to find the author who has had posted the most recent post, would I do something like above where I use functions inside an array? I want to sort the authors by most recent post and assign the authors to divs based on that order.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Use get_the_id() with an Array?’ is closed to new replies.