Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Latest post from category by author WordPress

  • SOLVED

Hi There, I want to show the latest post from category by author, to this point Im showing authors and their last post. Any help trying to show lastest post by author..


<strong>This is the code that I got... </strong>


<?php $authors = get_users(); ?>

<?php
$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', ' ' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'numberposts' => 1, '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(); ?>

<li>
<?php //userphoto_the_author_thumbnail(); ?>
<span class="blogauthor"> <?php the_author_posts_link(); ?> </span>
<div class="blog"> <a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
</li>

<?php endwhile; endif; ?>

<?php } } ?>




<strong>This is code is doing this </strong>

Author Name A
Lastest Post from Author A

Author B
Lastest Post from Author B

Author C
Lastest Post from Author C

Author X
Lastest Post from Author x



I want this....

Lastest Post from Category X
Post from Author B

Previous Lastest Post from Category X
Post from Author D

Previous Lastest Post from Category X
Post from Author F

Previous Lastest Post from Category X
Post from Author A

Previous Lastest Post from Category X
Post from Author B


What I think the problem is some nesting code. I think I have to look for the posts then the authors...

Sorry, my english isn't good

Answers (2)

2013-02-24

Sébastien | French WordpressDesigner answers:

the result is what you want if you use the template category.php and use the function <?php the_author(); ?> in this template.


Hola Politica comments:

Sorry that I didn't explain myself very clear... This is the loop that I want to do

From Category X get recent posts order by date

If is the first time this author is on the loop

Print Post Title and Authors Name

Else do nothing


Sébastien | French WordpressDesigner comments:

with this condition : has_cap('level_7') ?


Hola Politica comments:

Right now it doesn't matter


Sébastien | French WordpressDesigner comments:

try this


<?php

$authors = get_users();
//print_r($authors);
$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);

//print_r($latest_posts);

foreach ($latest_posts as $latest_post) {
echo '<li>
<span class="blogauthor">'.get_author_posts_url($latest_post->post_author).' </span>
<div class="blog"><a title="Permanent Link to '.$latest_post->post_title.'" href="'.get_permalink($latest_post->ID).'" rel="bookmark">'.$latest_post->post_title.'</a></div>
</li>';
}

?>


Hola Politica comments:

Thanks, this is what I wanted


2013-02-24

Kailey Lampert answers:

Here's some code: https://gist.github.com/trepmal/5021690

What's happening:
- get all users
- if user has level_7, get most recent post from them
- save that post id in an array, keyed by the date
- sort the array by date, most recent first
- use that array in a new query, get only those ids, keep sort order
- do the loop


Hola Politica comments:

Sorry that I didn't explain myself very clear... This is the loop that I want to do

From Category X get recent posts order by date

If is the first time this author is on the loop

Print Post Title and Authors Name

Else do nothing