Home › Forums › Support › List Authors by Total Post Count and display with Content Template Block Element
- This topic has 6 replies, 3 voices, and was last updated 3 years, 7 months ago by
Ying.
-
AuthorPosts
-
August 7, 2022 at 3:17 am #2305604
Yulia
Dear GP team,
We plan to have large number of contributors to our blog.
Each contributor has a customized profile page.
It’s created as a post inteamcustom post type.
Each profile page is authored/written by the contributors themselves.We’re trying to create a custom archive page for
teampost type.
We aim to list all the authors by order of the size of their contributions to the blog.
So the authors with most posts get to the top of the list.
Also, we’d like to use GP Content Block Element to style the archive page.We experimented using Query Loop Block Element for this, but it lacks the ‘sort by’ attribute we are looking for.
What is the best way to achieve this?
Thanks.
August 7, 2022 at 6:15 am #2305725Yulia
Found a way to get wordpress authors in order by the number of custom posts published.
Here is the code:
$authors = $wpdb->get_results( "SELECT $wpdb->users.ID AS author_id, $wpdb->users.display_name AS author_name, COUNT($wpdb->posts.ID) AS published_songs_and_poems, COUNT(CASE WHEN $wpdb->posts.post_type = 'song' THEN $wpdb->posts.ID ELSE NULL END) as published_songs, COUNT(CASE WHEN $wpdb->posts.post_type = 'poem' THEN $wpdb->posts.ID ELSE NULL END) as published_poems FROM $wpdb->users JOIN $wpdb->posts ON $wpdb->posts.post_author = $wpdb->users.ID AND $wpdb->posts.post_type IN('song', 'poem') AND $wpdb->posts.post_status = 'publish' GROUP BY author_id ORDER BY published_songs_and_poems DESC" );array_walk($authors, function($author) { echo $author->author_name." has published ".$author->published_songs." song(s) and ".$author->published_poems." poem(s).<br/>"; });We’ll adjust this code to include regular post type as well.
Is there a way to use Content Block Element that we already have designed to display the results?
It doesn’t have to be on the actual archive page of CPT. We can create another page to list the authors, if it’s too complicated to do this on the archive page.August 7, 2022 at 6:39 am #2305752David
StaffCustomer SupportHi there,
the Block element content template will work on any archive template that uses the
generate_do_template_partin the loop – see here for an example in our archive.php:Then you could use the
pre_get_postsfilter to pass in yourorderbyandorderparameters into the WP_Query.
Heres an example using a custom post meta value:August 13, 2022 at 12:33 am #2311571Yulia
Thank you, but I couldn’t get it to work.
So far I can sort by order of published date by setting order to
DESCorASC.
Settingorderby = post_countdoesn’t do anything.I’m bad with php and I couldn’t figure out how to write the snippet to do the following:
Author profiles live inteamCPT. Each profile is created by a WordPress user.
These users are also authors or regular blog postspost_type('post')
The goal is to display a list of author profiles (teamarchive) and sort them in order of number of regular blog posts they have contributed.This is the snippet I’ve been playing with:
function custom_cpt_order($query){ if( !is_admin() && ( is_post_type_archive('team') ) && empty( $query->query_vars['suppress_filters'] )){ $query->query_vars['meta_key'] = ''; $query->query_vars['orderby'] = 'post_count'; $query->query_vars['order'] = 'DESC'; } return $query; } add_filter( 'pre_get_posts', 'custom_cpt_order' );Thanks a lot for your help.
August 13, 2022 at 9:18 am #2311934David
StaffCustomer SupportSo this:
$query->query_vars['orderby'] = 'post_count';will only work if you were querying the Users.e.g
$users = get_users('orderby=post_count');So the question here is how do you get the
post_countfor each of the authors and store that in some custom post meta in your CPT. Not sure on this one.September 3, 2022 at 4:41 am #2332268Yulia
Hey guys, been on this problem for weeks now.
Any ideas?Cheers!
September 3, 2022 at 12:51 pm #2332640Ying
StaffCustomer SupportSo the question here is how do you get the post_count for each of the authors and store that in some custom post meta in your CPT.
David asked a question here, can you answer it so we can have a better understanding of the situation?
-
AuthorPosts
- You must be logged in to reply to this topic.