Ask your WordPress questions! Pay money and get answers fast! (more info)

Authors latest post sorted by post date...(Not repeated author) WordPress

  • SOLVED

Hi guys, Im trying to make this.. Authors latest post sorted by post date...(Not repeated author) So far Im able to do the first part but I dont know how to sort it by date... Can you take a look please


Something like this...

Post 1000 Author X,

Post 998 Author Z,

Post 980 Author C,

Post 932 Author N,

Post 870 Author F.....



<?php

$authors = get_users();
$latest_posts = array();

foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );
if ( user_can( $user, "author" ) ) {
$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'post_date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );
foreach ($ps as $p) {
$latest_posts[$p->post_date] = $p;
}
}

krsort($latest_posts);

?>

<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones'); ?>

<?php
$counter = 0;
foreach ($latest_posts as $post) {
$counter++;
if ($counter > 1)
break;
setup_postdata($post);
?>

<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<div class="author-post">
<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>
<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><p>
<p><span class="author"><?php the_author_posts_link(); ?></span><p>
</div>
<div class="clear"></div>


<?php endwhile; endif; ?>

<?php } } ?>

Answers (4)

2013-12-30

Hariprasad Vijayan answers:

Hello,

Try this code

<?php
$authors = array();
$args=array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category_name'=> 'opiniones'
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$author_id=$my_query->post->post_author;
$user = new WP_User( $author_id);
if ( user_can( $user, "author" ) ) {
if (!in_array($author_id,$authors)) {
$myposts = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'post_date', 'post_type' => 'post', 'posts_per_page' => 1, 'post_status' => 'publish', 'author' => $author_id ) );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="author-post"> <?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>
<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<p>
<p><span class="author">
<?php the_author_posts_link(); ?>
</span>
<p>
</div>
<div class="clear"></div>
<?php endforeach;
wp_reset_postdata();
$authors[]=$author_id;
} }
endwhile;
}
wp_reset_query();
?>

It is entirely different code and it works in my localhost.


Hariprasad Vijayan comments:

Is it works for you? Or any trouble? Let me know


Hola Politica comments:

You got it, and I love how its coded!!


Hola Politica comments:

Its working and this is what I wanted it. Thanks!!


Hariprasad Vijayan comments:

Happy to help you. :-)

2013-12-30

Arnav Joy answers:

try this

<?php



$authors = get_users();

$latest_posts = array();



foreach ( $authors as $author ) {

$user = new WP_User( $author->ID );

if ( user_can( $user, "author" ) ) {

$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );

foreach ($ps as $p) {

$latest_posts[$p->post_date] = $p;

}

}



krsort($latest_posts);



?>



<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones'); ?>



<?php

$counter = 0;

foreach ($latest_posts as $post) {

$counter++;

if ($counter > 1)

break;

setup_postdata($post);

?>



<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>



<div class="author-post">

<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>

<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><p>

<p><span class="author"><?php the_author_posts_link(); ?></span><p>

</div>

<div class="clear"></div>





<?php endwhile; endif; ?>



<?php } } ?>


Arnav Joy comments:

<?php



$authors = get_users();

$latest_posts = array();



foreach ( $authors as $author ) {

$user = new WP_User( $author->ID );

if ( user_can( $user, "author" ) ) {

$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );

foreach ($ps as $p) {

$latest_posts[$p->post_date] = $p;

}

}



krsort($latest_posts);



?>



<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones&orderby=date&order=desc'); ?>



<?php

$counter = 0;

foreach ($latest_posts as $post) {

$counter++;

if ($counter > 1)

break;

setup_postdata($post);

?>



<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>



<div class="author-post">

<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>

<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><p>

<p><span class="author"><?php the_author_posts_link(); ?></span><p>

</div>

<div class="clear"></div>





<?php endwhile; endif; ?>



<?php } } ?>


Hola Politica comments:

Changing 'orderby' => 'post_date' to 'orderby' => 'date', didnt make any change, not by post date

Authors are still ordered alphabetically, you can take a look to the script running at holapolitica.com.. Opiniones Widget.

2013-12-30

Sai kumar answers:

Just change this :

$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'post_date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );


to

$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );


Sai kumar comments:

Change this <?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones'); ?>

to

<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones&orderby=date&order=ASC'); ?>


Sai kumar comments:

Same as Mr.Arnav Mentioned to you


Hola Politica comments:

Changing 'orderby' => 'post_date' to 'orderby' => 'date', didnt make any change, not by post date

Authors are still ordered alphabetically, you can take a look to the script running at holapolitica.com.. Opiniones Widget.


Sai kumar comments:

did u changed this


<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones'); ?>


to


<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones&orderby=date&order=ASC'); ?>


Hola Politica comments:

Yup

Right now is working with this code.... And I think the problem might be over the krsort function


<?php

$authors = get_users();
$latest_posts = array();



foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );
if ( user_can( $user, "author" ) ) {
$ps = get_posts( array( 'category_name' => 'opiniones', 'orderby' => 'date', 'post_type' => 'post', 'numberposts' => 1, 'post_status' => 'publish', 'author' => $author->ID ) );
foreach ($ps as $p) {
$latest_posts[$p->date] = $p;
}
}

krsort($latest_posts);

?>

<?php query_posts('author=' . $author->ID . '&showposts=1&category_name=opiniones&orderby=date&order=ASC'); ?>

<?php
$counter = 0;
foreach ($latest_posts as $post) {
$counter++;
if ($counter > 1)
break;
setup_postdata($post);
?>

<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>

<div class="author-post">
<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>
<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><p>
<span class="date"><i class="icon-calendar"></i> <?php echo get_the_date('l, j \d\e F, Y'); ?></span>
<p><span class="author"><?php the_author_posts_link(); ?></span><p>
</div>
<div class="clear"></div>


<?php endwhile; endif; ?>

<?php } } ?>


Sai kumar comments:

If you don't mind please try with this code please.



<?php



$authors = get_users();

$latest_posts = array();



foreach ( $authors as $author ) {



$user = new WP_User( $author->ID );



if ($user->has_cap('level_7')) {


$ps = get_posts(

array(

'category_name' => 'ile-de-re',

'orderby' => 'post_date',

'order' => 'DESC',

'post_type' => 'post',

'numberposts' => 1,

'author' => $author->ID

)

);



foreach ($ps as $p) {

$latest_posts[strtotime($p->post_date)] = $p;

}



}



}



krsort($latest_posts);





foreach ($latest_posts as $latest_post) {

<div class="author-post">

<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left'); ?>

<p><a class="title" title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><p>

<span class="date"><i class="icon-calendar"></i> <?php echo get_the_date('l, j \d\e F, Y'); ?></span>

<p><span class="author"><?php the_author_posts_link(); ?></span><p>

</div>

<div class="clear"></div>
}
?>



I think it will work. Please do backup your old code before editing. Please try it and get back to me


Sai kumar comments:

Sorry

little modifications





<?php







$authors = get_users();



$latest_posts = array();







foreach ( $authors as $author ) {







$user = new WP_User( $author->ID );







if ($user->has_cap('level_7')) {





$ps = get_posts(



array(



'category_name' => 'ile-de-re',



'orderby' => 'post_date',



'order' => 'DESC',



'post_type' => 'post',



'numberposts' => 1,



'author' => $author->ID



)



);







foreach ($ps as $p) {



$latest_posts[strtotime($p->post_date)] = $p;



}







}







}







krsort($latest_posts);











foreach ($latest_posts as $latest_post) {


echo '
<div class="author-post">



.'get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left').'



<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark"><?php the_title(); ?></a><p>



<span class="date"><i class="icon-calendar"></i> '.get_the_date('l, j \d\e F, Y').'</span>



<p><span class="author">'.get_author_posts_url($latest_post->post_author).'</span><p>



</div>



<div class="clear"></div> ';

}

?>




Try this Please


Sorry for the wrong code...


Sai kumar comments:

Please correct


<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark"><?php the_title(); ?></a><p>



with

<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark">'.$latest_post->post_title.'</a><p>


Apology for my mistake please do copy this final code from here




<?php

$authors = get_users();

$latest_posts = array();

foreach ( $authors as $author ) {

$user = new WP_User( $author->ID );
if ($user->has_cap('level_7')) {
$ps = get_posts(
array(
'category_name' => 'ile-de-re',
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'numberposts' => 1,
'author' => $author->ID
)
);

foreach ($ps as $p) {
$latest_posts[strtotime($p->post_date)] = $p;
}
}
}
krsort($latest_posts);

foreach ($latest_posts as $latest_post) {
echo '
div class="author-post">
.'get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left').'
<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark">'.$latest_post->post_title.'</a><p>
<span class="date"><i class="icon-calendar"></i> '.get_the_date('l, j \d\e F, Y').'</span>
<p><span class="author">'.get_author_posts_url($latest_post->post_author).'</span><p>
</div>
<div class="clear"></div> ';
}


Sai kumar comments:

Please use this code and reply me



<?php



$authors = get_users();



$latest_posts = array();



foreach ( $authors as $author ) {



$user = new WP_User( $author->ID );

if ($user->has_cap('level_7')) {

$ps = get_posts(

array(

'category_name' => 'ile-de-re',

'orderby' => 'post_date',

'order' => 'DESC',

'post_type' => 'post',

'numberposts' => 1,

'author' => $author->ID

)

);



foreach ($ps as $p) {

$latest_posts[strtotime($p->post_date)] = $p;

}

}

}

krsort($latest_posts);



foreach ($latest_posts as $latest_post) {

echo '

<div class="author-post">

.'get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left').'

<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark">'.$latest_post->post_title.'</a><p>

<span class="date"><i class="icon-calendar"></i> '.get_the_date('l, j \d\e F, Y').'</span>

<p><span class="author">'.get_author_posts_url($latest_post->post_author).'</span><p>

</div>

<div class="clear"></div> ';

}


Sai kumar comments:

Hola Politica. Once again I am really sorry. There was some errors on the code i given. Just corrected all those and upload here. Please try with this one if you dont mind. Sorry once again.



<?php

$authors = get_users();
$latest_posts = array();

foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );
if ($user->has_cap('level_7')) {
$ps = get_posts(
array(
'category_name' => 'opiniones',
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => 1,
'author' => $author->ID
)
);

foreach ($ps as $p) {
$latest_posts[strtotime($p->post_date)] = $p;
}
}
}

krsort($latest_posts);

foreach ($latest_posts as $latest_post) {
echo '
<div class="author-post">
'.get_wp_user_avatar(get_the_author_meta('ID'), 85, 'left').'
<p><a class="title" title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark">'.$latest_post->post_title.'</a><p>
<span class="date"><i class="icon-calendar"></i> '.get_the_date('l, j \d\e F, Y').'</span>
<p><span class="author">'.get_author_posts_url($latest_post->post_author).'</span><p>
</div>
<div class="clear"></div> ';
}


Sai kumar comments:

Please email me at : [email protected]


Hola Politica comments:

That code looks like a previous questions I was doing here a time ago http://wpquestions.com/question/show/id/8068 02/23/13 at 10:08pm


Hola Politica comments:

Still didn't work


Sai kumar comments:

What's the problem now..??


Hola Politica comments:

Right now I Test widget.. and is just showing a date and http://www.holapolitica.com/author/ ... I did try to change and remove the author level... didnt work

2013-12-31

Dbranes answers:

Great that @Hariprasad solved it for you.

<strong>ps:</strong>

Given the restriction not to write a SQL query and minimize the number of database calls, one could construct a <em>WP_Query()</em> call with the function <strong>wpq_last_post_per_user()</strong>:


$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'post__in' => wpq_last_post_per_user( array( 'category_name' => 'opiniones' ) ),
);

$query = new WP_Query( $args );
if( $query->have_posts() ):
while ( $query->have_posts() ) : $query->the_post();
?>
<div class="author-post">
... the loop display ...
</div>

<?php
endwhile;
endif;




where the <em>wpq_last_post_per_user()</em> is defined as:


/**
* Fetch the last post ID for each author, in a single get_posts() call,
*
* @link http://www.wpquestions.com/question/showLoggedIn/id/9117
*
* @param array $args Arguments for get_posts
* @return array Array containing posts id's
*/
function wpq_last_post_per_user( $args = array() )
{
$defaults = array(
'posts_per_page' => 10,
'post_type' => 'post',
'orderby' => 'ID',
'order' => 'DESC',
'suppress_filters' => FALSE,
);

$args = wp_parse_args( $args, $defaults );

add_filter( 'posts_groupby', $f = function( $groupby ) use ( &$f ) {
remove_filter( current_filter(), $f );
$groupby = sprintf( '%s.post_author', $GLOBALS['wpdb']->posts );
return $groupby;
} );

add_filter( 'posts_fields', $f = function( $fields ) use ( &$f ){
remove_filter( current_filter(), $f );
$fields = sprintf( '%s, MAX( %s.ID ) as maxid ', $fields, $GLOBALS['wpdb']->posts );
return $fields;
} );

return wp_list_pluck( get_posts( $args ), 'maxid' );
}