Skip Navigation

[Resolved] Sort Custom Post Types in View based on CPT Author User Meta Storing Number

This support ticket is created 3 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by andrei-laurentiuP 3 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#1615115

I am trying to sort Custom Post Types in View based on author's User Meta storing a number.

Is there a direct way or should I use code?

Maybe using somehow wpv_filter_query + $author_points = get_the_author_meta('user_points_count, $author_id);

Or what would your suggestion be?

#1615637

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Andrei,

Can I get some clarity on this ?

Is it that you want to sort your Authors based on a custom field and then display the posts of those sorted authors ?

Will you be listing out all the authors ?

Please let me know.
Thanks,
Shane

#1616573

Hey Shane! Thanks for taking over this.

I was wondering, if possible, to sort the CPTs based on a user's meta key storing a number.

For example User_X has a point count of 10. And we have User_Y with 12 points. User_X has CPT1 and CPT2 and User_Y CPT3 and CPT4. I would like, for ex to 'orderby' 'DESC', to see CPT3 and CPT4 first, and CPT1 and CPT2 after. i.e. Posts of user with more points will be in front.

To sort the users, the code should be:

add_filter( 'wpv_filter_user_query', 'custom_orderby_func', 100, 3 );

function custom_orderby_func( $query_args, $view_settings, $view_id ) {
if ( $view_id == 999 ) {
$query_args['meta_key'] = 'user_points_count';
$query_args['orderby'] = 'meta_value';
$query_args['order'] = 'DESC';
}
return $query_args;
}

I could list & sort the users with a view and try to have a view for the cpts within the users view, but I don't know how to add a proper filter (to display CPTs of User1, CPTs of User2, and so on).

Maybe it's an idea to create a custom field for the CPTs and to transfer the user_points_count to the CPTs of the user. And then sort the view based on that custom field. I don't know if this is the normal approach or if there is any other option (to have minimum queries).

Hope you are safe & well.

Cheers,
Andrei

#1617009

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2020-05-08 at 9.22.36 AM.png
Screenshot 2020-05-07 at 3.30.10 PM.png

Hi Andrei,

This would be better done with a nested view setup.

So you first view will list all the authors as you want it and add the sort option to the Author view so that it sorts the author by the custom field.

Secondly you create a post view to list the posts. Then you will add an author query filter to the view by clicking the add a filter button. See Screenshot

Finally you will set it to get the author from the parent view. See Screenshot

Then just add this Post view to the Authors view within the view's content template and you should be seeing your posts sorted in the order that you want it.

Thanks,
Shane

#1617043

Pff.. I missed the parent User View option. My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.