admin管理员组文章数量:1130349
We are introducing a "featured author" area on our site and would like to display the most recent articles by a select group of authors. However, we only want a maximum of one post per author to display. So an author could potentially have posted 5 times since another author had posted, but regardless only one of their posts should appear. Currently this is the code I've got:
<?php
$args = array(
'showposts' => 5,
'author' => "6800,3845,1720,7045,4949"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
// DISPLAYING STUFF
<?php endwhile; wp_reset_query(); ?>
One potential solution I have considered is querying more posts and setting up an array, then checking the array each time to see if an author is already in it. If they are, it would continue to the next row. But an obvious issue with this would be that I may potentially end up having to pull back 100's of posts if a particular "featured author" hadn't wrote for a while.
I'm still fairly new to PHP/MySQL and a solution is probably starring me in the face. Appreciate any assistance.
We are introducing a "featured author" area on our site and would like to display the most recent articles by a select group of authors. However, we only want a maximum of one post per author to display. So an author could potentially have posted 5 times since another author had posted, but regardless only one of their posts should appear. Currently this is the code I've got:
<?php
$args = array(
'showposts' => 5,
'author' => "6800,3845,1720,7045,4949"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
// DISPLAYING STUFF
<?php endwhile; wp_reset_query(); ?>
One potential solution I have considered is querying more posts and setting up an array, then checking the array each time to see if an author is already in it. If they are, it would continue to the next row. But an obvious issue with this would be that I may potentially end up having to pull back 100's of posts if a particular "featured author" hadn't wrote for a while.
I'm still fairly new to PHP/MySQL and a solution is probably starring me in the face. Appreciate any assistance.
Share Improve this question asked Jan 3, 2013 at 15:25 Peter Willis IIIPeter Willis III 915 bronze badges 1 |3 Answers
Reset to default 6You need to GROUP BY the author ID, which is going to require a filter on posts_groupby. The Codex page for that filter is non-existent but it works like posts_join. Something like...
function filter_authors($groupby) {
global $wpdb;
$groupby = " {$wpdb->posts}.post_author";
return $groupby;
}
add_filter('posts_groupby','filter_authors');
$args = array(
'showposts' => 3,
'author' => "1,2,3"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo $post->post_title.' :: '.$post->post_author."<br/>";
endwhile;
Use your own values in $args of course.
that will effect any other query on the page that runs after this block of code. You might want to remove the filter after you are done.
remove_filter('posts_groupby','filter_authors');
Another approach could be to use a function that loops through get_posts and prints the result.
Check this Q&A: When should you use WP_Query vs query_posts() vs get_posts()?
// function located in the theme's functions.php
function wpse_78117_print_authors_last_post()
{
$user_ids = array( '1', '2' );
foreach( $user_ids as $user )
{
$args = array(
'post_type' => 'post',
'numberposts' => 1,
'author' => $user
);
// as we are getting only 1 post, extract it from the returned array
$user_post = array_shift( get_posts( $args ) );
// similar
$nick = array_shift( get_user_meta( $user, 'nickname' ) );
// custom output, $user_post contains all the post normal data
echo $user_post->post_title . ', by: ' . $nick;
}
}
And then use it in any theme template:
<?php wpse_78117_print_authors_last_post(); ?>.
I had this problem for only wanting one post per author to put into a slider, and I resolved it like this:
$query_args = array('post_type' => 'post');
$query = new WP_Query($query_args);
$author_ids = array(); // Array of author id's to check before executing
if ($query->have_posts()) : $query->the_post();
if (!in_array(get_the_author_meta('ID'), $author_ids)) {
// DO YOUR STUFF
// Afterwards, add that id to our array so we don't get another one
array_push($author_ids, get_the_author_meta('ID'));
}
endif;
We are introducing a "featured author" area on our site and would like to display the most recent articles by a select group of authors. However, we only want a maximum of one post per author to display. So an author could potentially have posted 5 times since another author had posted, but regardless only one of their posts should appear. Currently this is the code I've got:
<?php
$args = array(
'showposts' => 5,
'author' => "6800,3845,1720,7045,4949"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
// DISPLAYING STUFF
<?php endwhile; wp_reset_query(); ?>
One potential solution I have considered is querying more posts and setting up an array, then checking the array each time to see if an author is already in it. If they are, it would continue to the next row. But an obvious issue with this would be that I may potentially end up having to pull back 100's of posts if a particular "featured author" hadn't wrote for a while.
I'm still fairly new to PHP/MySQL and a solution is probably starring me in the face. Appreciate any assistance.
We are introducing a "featured author" area on our site and would like to display the most recent articles by a select group of authors. However, we only want a maximum of one post per author to display. So an author could potentially have posted 5 times since another author had posted, but regardless only one of their posts should appear. Currently this is the code I've got:
<?php
$args = array(
'showposts' => 5,
'author' => "6800,3845,1720,7045,4949"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
// DISPLAYING STUFF
<?php endwhile; wp_reset_query(); ?>
One potential solution I have considered is querying more posts and setting up an array, then checking the array each time to see if an author is already in it. If they are, it would continue to the next row. But an obvious issue with this would be that I may potentially end up having to pull back 100's of posts if a particular "featured author" hadn't wrote for a while.
I'm still fairly new to PHP/MySQL and a solution is probably starring me in the face. Appreciate any assistance.
Share Improve this question asked Jan 3, 2013 at 15:25 Peter Willis IIIPeter Willis III 915 bronze badges 1-
wp_reset_queryis intended to be used after callingquery_posts, you probably meant to usewp_reset_postdata– Tom J Nowell ♦ Commented Jun 5, 2015 at 19:47
3 Answers
Reset to default 6You need to GROUP BY the author ID, which is going to require a filter on posts_groupby. The Codex page for that filter is non-existent but it works like posts_join. Something like...
function filter_authors($groupby) {
global $wpdb;
$groupby = " {$wpdb->posts}.post_author";
return $groupby;
}
add_filter('posts_groupby','filter_authors');
$args = array(
'showposts' => 3,
'author' => "1,2,3"
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo $post->post_title.' :: '.$post->post_author."<br/>";
endwhile;
Use your own values in $args of course.
that will effect any other query on the page that runs after this block of code. You might want to remove the filter after you are done.
remove_filter('posts_groupby','filter_authors');
Another approach could be to use a function that loops through get_posts and prints the result.
Check this Q&A: When should you use WP_Query vs query_posts() vs get_posts()?
// function located in the theme's functions.php
function wpse_78117_print_authors_last_post()
{
$user_ids = array( '1', '2' );
foreach( $user_ids as $user )
{
$args = array(
'post_type' => 'post',
'numberposts' => 1,
'author' => $user
);
// as we are getting only 1 post, extract it from the returned array
$user_post = array_shift( get_posts( $args ) );
// similar
$nick = array_shift( get_user_meta( $user, 'nickname' ) );
// custom output, $user_post contains all the post normal data
echo $user_post->post_title . ', by: ' . $nick;
}
}
And then use it in any theme template:
<?php wpse_78117_print_authors_last_post(); ?>.
I had this problem for only wanting one post per author to put into a slider, and I resolved it like this:
$query_args = array('post_type' => 'post');
$query = new WP_Query($query_args);
$author_ids = array(); // Array of author id's to check before executing
if ($query->have_posts()) : $query->the_post();
if (!in_array(get_the_author_meta('ID'), $author_ids)) {
// DO YOUR STUFF
// Afterwards, add that id to our array so we don't get another one
array_push($author_ids, get_the_author_meta('ID'));
}
endif;
本文标签: Query to return maximum of one post per author
版权声明:本文标题:Query to return maximum of one post per author 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749241122a2338107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_reset_queryis intended to be used after callingquery_posts, you probably meant to usewp_reset_postdata– Tom J Nowell ♦ Commented Jun 5, 2015 at 19:47