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

One Post Per Author WordPress

  • SOLVED

Hi,
query below is very simple: returns only 12 posts for particular date and order by meta_key, and works fine.


$monthnum = date('m');
$day = date('d');
if (have_posts()) :
$args=array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 12,
);
query_posts($args);
while (have_posts()) : the_post();
include ( TEMPLATEPATH . '/loop-big.php' );
endwhile; endif; wp_reset_query();


I've many authors and many times query display post by the same author.

When there are more posts by author I'd show only post per author, sort by meta key value.

Is possible?

Sorry for my bad eng.

Thanks.
Marco

Answers (2)

2013-03-04

Sébastien | French WordpressDesigner answers:

is it for your site http://www.holapolitica.com/ ?


microtag comments:

No, it isn't.


Sébastien | French WordpressDesigner comments:

i suggest to increase your fee


microtag comments:

how much?



Sébastien | French WordpressDesigner comments:

try this :

<?php
$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');

if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );

$ps = get_posts(
array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 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 $post) {
include ( TEMPLATEPATH . '/loop-big.php' );
}
?>


microtag comments:

Do not display nothing.
I want to decide how many posts show per page: 'showposts' => 12,


Sébastien | French WordpressDesigner comments:

replace

foreach ($latest_posts as $post) {

include ( TEMPLATEPATH . '/loop-big.php' );

}

by

foreach ($latest_posts as $latest_post) {

echo "example : " . $latest_post->post_title . " <BR>";
include ( TEMPLATEPATH . '/loop-big.php' );

}


Sébastien | French WordpressDesigner comments:

this is the perfect code for you :

$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');
$i=0;
if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );
$args = array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 1,
'author' => $author->ID
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
$i++;
//include ( TEMPLATEPATH . '/loop-big.php' );
echo $post->post_author;
echo"<br>";
echo $post->post_title;
echo"<br><br><br><br>";
endwhile; endif; wp_reset_query();
if ($i==12) break;
}
}


Sébastien | French WordpressDesigner comments:

i have tested this code on my site.
It's OK.
This code return
the post author ID and the post title.
If it is the case on your site, the code works.
In this case, replace
//include ( TEMPLATEPATH . '/loop-big.php' );

echo $post->post_author;

echo"<br>";

echo $post->post_title;

echo"<br><br><br><br>";

by
include ( TEMPLATEPATH . '/loop-big.php' );


Sébastien | French WordpressDesigner comments:

@WisdmLabs : try to write your self code, please.


Sébastien | French WordpressDesigner comments:

@WisdmLabs : ok, no problem :-)


microtag comments:

@Sébastien Now loop works, but not order by meta-key... post are alphabetical order by the authors.

@WisdmLabs loop not works and "print_r" have same problem... Post Author alphabetical order


Sébastien | French WordpressDesigner comments:

this code is ok :-)


$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');
$i=0;

$latest_posts = array();

if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );

$ps = get_posts(
array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 1,
'author' => $author->ID
)
);
foreach ($ps as $p) {
$latest_posts[get_post_meta($p->ID,'tweets_count',true)] = $p;
}
}
}
krsort($latest_posts);
foreach ($latest_posts as $post) {
$i++;
//include ( TEMPLATEPATH . '/loop-big.php' );
echo $post->post_author;
echo"<br>";
echo $post->post_title;
echo"<br><br><br><br>";
}


Sébastien | French WordpressDesigner comments:

same thing :


This code return the post author ID and the post title (now the posts are ordered by meta_value)
If it is the case on your site, the code works.
In this case, replace

//include ( TEMPLATEPATH . '/loop-big.php' );



echo $post->post_author;



echo"<br>";



echo $post->post_title;



echo"<br><br><br><br>";



by

include ( TEMPLATEPATH . '/loop-big.php' );


Sébastien | French WordpressDesigner comments:

i have forgotten if ($i==12) break;
i add this line and the final code is :

$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');
$i=0;

$latest_posts = array();

if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );

$ps = get_posts(
array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 1,
'author' => $author->ID
)
);
foreach ($ps as $p) {
$latest_posts[get_post_meta($p->ID,'tweets_count',true)] = $p;
}
}
}
krsort($latest_posts);
foreach ($latest_posts as $post) {
$i++;
//include ( TEMPLATEPATH . '/loop-big.php' );
echo $post->post_author;
echo"<br>";
echo $post->post_title;
echo"<br><br><br><br>";
if ($i==12) break;
}


microtag comments:

Thanks Sébastien, works fine.
I still want to change the code above with new modify.
How do you prefer that I increase money or open new question?
Thanks, and very sorry for my bad eng.
Marco


Sébastien | French WordpressDesigner comments:

no problem :-)

you write :
<blockquote>I still want to change the code above with new modify.</blockquote>
explain to me, please. Which code ? which modification ?


microtag comments:

I'd add pagination to your code.

If I remove if ($i==12) break display 70 post but I'd load post with infinite scroll plugin (http://wordpress.org/extend/plugins/infinite-scroll/).

I've tried plugin (with simple query post) and works fine, i must add twentyeleven_content_nav( 'nav-below' ); after endwhile; and load new post with scroll down.

Is possible add this features to your code?

Thanks


Sébastien | French WordpressDesigner comments:

yes twentyeleven_content_nav( 'nav-below' ); can't work wih this code
I can write another code, for this second question.
Increase your fee 20=>35$ ans I do that now.


microtag comments:

thanks


Sébastien | French WordpressDesigner comments:

Try this code

<?php
$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');
$i=0;

$latest_posts = array();

if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );

$ps = get_posts(
array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
//'monthnum' => $monthnum,
//'day' => $day,
//'showposts' => 1,
'author' => $author->ID
)
);

foreach ($ps as $p) {
if($p->ID) $latest_posts[get_post_meta($p->ID,'tweets_count',true)] = $p->ID;
}
}
}
ksort($latest_posts);

$args = array(
'post__in' => $latest_posts,
'showposts' => 12
);

$query = new WP_Query($args);
if($query->have_posts()) :

twentyeleven_content_nav( 'nav-above' );

while($query->have_posts()) : $query->the_post();

echo $post->post_author;
echo"<br>";
echo $post->post_title;
echo"<br><br><br><br>";

endwhile;
twentyeleven_content_nav( 'nav-below' );
else :
endif; ?>



This code return the post author ID and the post title (now the posts are ordered by meta_value)
If it is the case on your site, the code works.
In this case, replace

//include ( TEMPLATEPATH . '/loop-big.php' );
echo $post->post_author;
echo"<br>";
echo $post->post_title;
echo"<br><br><br><br>";


by

include ( TEMPLATEPATH . '/loop-big.php' );


Sébastien | French WordpressDesigner comments:

as you can see this code use a loop, a query and the function twentyeleven_content_nav( 'nav-below' );


microtag comments:

Infinite scroll works fine but now I've some posts by same author.


microtag comments:

... and not order by meta-key.


Sébastien | French WordpressDesigner comments:

arf... sorry :
replace
array(

'meta_key' => 'tweets_count',

'orderby' => 'meta_value_num',

//'monthnum' => $monthnum,

//'day' => $day,

//'showposts' => 1,

'author' => $author->ID

)


by

array(

'meta_key' => 'tweets_count',

'orderby' => 'meta_value_num',

'monthnum' => $monthnum,

'day' => $day,

'showposts' => 1,

'author' => $author->ID

)


Sébastien | French WordpressDesigner comments:

remove //
3 times


microtag comments:

No, not works.
Not order by meta-key and after first scroll/loading displays the same posts of first page


Sébastien | French WordpressDesigner comments:

you need to use the variable $paged

The code will be like this :


<?php
get_header(); ?>

<div id="primary">
<div id="content" role="main">

<?php
$authors = get_users();
$latest_posts = array();
$monthnum = date('m');
$day = date('d');
$i=0;

$latest_posts = array();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

if($authors) {
foreach ( $authors as $author ) {
$user = new WP_User( $author->ID );

$ps = get_posts(
array(
'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',
'monthnum' => $monthnum,
'day' => $day,
'showposts' => 1,
'author' => $author->ID,
)
);

foreach ($ps as $p) {
if($p->ID) $latest_posts[get_post_meta($p->ID,'tweets_count',true)] = $p->ID;
}
}
}
ksort($latest_posts);

print_r($latest_posts);
$args = array(
'post__in' => $latest_posts,
'paged' => $paged,
);

query_posts( $args );


if ( have_posts() ) :

twentyeleven_content_nav( 'nav-above' );

while ( have_posts() ) : the_post();
echo '<article class="post">';
echo "ID of the author : " . $post->post_author;
echo"<br>";
echo "title of his post : <h3>" . $post->post_title . "</h3>";
echo '</article>';
echo"<br><br>";
endwhile;

twentyeleven_content_nav( 'nav-below' );

else :

endif; ?>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>



in my case the content of the loop is

echo '<article class="post">';
echo "ID of the author : " . $post->post_author;
echo"<br>";
echo "title of his post : <h3>" . $post->post_title . "</h3>";
echo '</article>';
echo"<br><br>";

and in the settings of the plugin infinite-scroll the setting for "item selector" is "#content article.post"

You can change the content of the loop. Be careful : see the settings of your plugin to know what is the item selector and to verify that the item selector is ok in your loop.


microtag comments:


ksort($latest_posts);

print_r($latest_posts);
$args = array(

'meta_key' => 'tweets_count',
'orderby' => 'meta_value_num',

'post__in' => $latest_posts,
'paged' => $paged,
);



I've add 'meta_key' => 'tweets_count', 'orderby' => 'meta_value_num', in second array and now order by meta key.

Thanks a lot.

Marco

2013-03-04

WisdmLabs answers:

Try this

$users = get_users();
$monthnum = date('m');
$day = date('d');
$i = 0;

if($users) {
foreach ( $users as $user )
{
$posts = get_posts(

array(

'meta_key' => 'tweets_count',

'orderby' => 'meta_value_num',

'monthnum' => $monthnum,

'day' => $day,

'showposts' => 1,

'author' => $user->ID

)

);

foreach ($posts as $p)
{
$author_post = $p;
}

$author_posts[$i] = $author_post;

$i++;
}

}

$show_posts = 12; //change value here to decide how many posts to show per page

for($i=0; $i<$show_posts; $i++)
{
//print_r($author_posts[$i]->post_title); you can test the output by removing comments from here
include ( TEMPLATEPATH . '/loop-big.php' );
}


WisdmLabs comments:

@Sébastien : our idea was just to enhance the code which you had posted at '11:05pm'. Before writing my code, I thought to test your code on my site so that I could upvote you answer but I found that few improvements were needed. So, I could not upvote your answer rather created a new modified script and tested it. To avoid any confusion, I just posted the full code instead of pointing each and every change(according to me) in your code. But, unfortunately when I posted my answer I did not concentrated the answer posted by you at '1:57pm'. I realize that it is something similar to what I have posted. Please believe I did not see your updated code and sorry for that.






microtag comments:

@Sébastien Now loop works, but not order by meta-key... post are alphabetical order by the authors.

@WisdmLabs loop not works and "print_r" have same problem... Post Author alphabetical order