What i want to get and have no clue how start is:

Query posts (for example from specific category, ordered by date) but to have the posts from same author numbered by there count in that posts list.

For example:

Date: 1/8/2017 Title: Great post Author: John Number: 1

Date: 1/7/2017 Title: Thrilling post Author: Mike Number: 1

Date: 1/6/2017 Title: New post Author: John Number: 2

Date: 1/5/2017 Title: Exiting post Author: Nathan Number: 1

Date: 1/4/2017 Title: Best post Author: Gandi Number: 1

Date: 1/3/2017 Title: Boring post Author: John Number: 3

Date: 1/2/2017 Title: Amazing post Author: Gandi Number: 2

Date: 1/1/2017 Title: Another post Author: Michael Number: 1

Of course i'm looking for an elegant, shortest code possible, simple way.

Edit: I thought of an idea: Maybe i can somehow use the author id as a new variable name, adding to this variable each post loop (while) and echoing it.

So i tried:

++${the_author_meta( ID )}; echo ${the_author_meta( ID )};

which i thought would create a variable named by the author id (for example: $465) and would add to it 1 (so $465 = 1) and echo '1'. but it doesn't :) actually ++${the_author_meta( ID )}; it self echoes the author id twice...

2 Answers 2


You could have an authors totals array and on each iteration you would increment that author's key:

<?php foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <?php $author_count = isset( $authors_totals[ get_the_author_meta('ID') ] ) ? int_val( $authors_totals[ get_the_author_meta('ID') ] ) + 1 : 1; ?>
    Date: <?php the_date('d/m/Y'); ?> 
    Title: <?php the_title(); ?> 
    Author: <?php the_author(); ?>: <?php echo $author_count; ?>    
<?php endforeach; ?>

It sounds like you can just keep a counter of which authors already were in the list like so

// Counting
$authors_printed = array();
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {

        $author = get_the_author();
        if (empty($authors_printed[ $author ])) {
            $authors_printed[ $author ] = 1;
        } else {
            $authors_printed[ $author ]++;
        // now $authors_printed[ $author ] has the correct number
        // more code..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.