Currently, it looks like you are looping through comments and then querying the posts from there. Could you use the comment_count
order by parameter directly in the posts query itself?
For example:
<?php
$post_args = array(
'post_type' => 'portfolio',
'posts_per_page' => 1,
'orderby' => 'comment_count'
);
$posts = get_posts($post_args);
foreach ($posts as $post) : setup_postdata($post);
$title = get_the_title($comment->comment_post_ID);
?>
<div class="col-md-3 col-sm-6 col-xs-12">
<h4><?php echo $title; ?></h4>
</div>
<?php
endforeach;
Source: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
Related Posts:
- Adding ‘menu order’ column to custom post type admin screen
- Custom post type – order field
- Displaying a custom post type alphabetically
- Order by & include array by specific post ids
- List all posts commented by current user
- Ordering posts having multiple post-meta date fields
- Custom Comment Types
- modify all posts of a category to “no comments allowed”
- Order Posts by Taxonomy and Meta Value
- Custom comment status possible?
- WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
- Custom Post type sort order not working in the admin area
- Add comments meta fields to comments metabox on post edit screen
- List upcoming events, ordered by date in a custom field
- ascending order custom post type
- WP_Query post at custom position
- Sort custom post types by last name in the backend
- wp_query and comment_parent – select only posts with top level comments
- changing default comment form arguments [duplicate]
- query order by date on custom type: wrong order
- Taxonomy list. Order by a specific custom post type count
- Re-order posts in query after
- Display custom post types with custom date field value (before today) & order by custom date field
- Disable comments from showing up public for Custom Post Type
- Custom Post Type Pagination & duplicate posts
- Custom Post Type loops and Disqus
- Separate comment section for post type in dashboard
- Count all comments of a custom post type
- How to quickly reorder posts in the admin panel that will persist for the wp-api
- How do I make a custom taxonomy for a CPT appear inbetween title and editor boxes?
- Insert data on comment post
- Combining sorted and random CPT
- Show listings from Impress Listing plugin in random order using taxonomy and terms
- I did group my search results by post type, but how can i give each of them its own order?
- Ordering posts in Search & taxonomy by post_meta
- get_comments not working on custom post types
- How to set order for a custom post type
- Sort CPT archive by order prevents sorting in admin
- WordPress comments_open for specific user role or post type
- Allow duplicate slugs for custom post type with taxonomies
- Add custom field in comments form
- How to order and group custom post type posts by year and month
- Order Custom Post Type by Custom Field Value
- How can the recipients and content for comment email notifications on a custom post type be changed?
- Get the taxonomy of a post hierarchically
- Change sort order when using ‘orderby’ => ‘type’
- Convert comments to Custom Post Type
- Ordering custom post type by multiple custom fields
- Manage Columns : order by post_parent’s title
- Order by custom field meta_key date and then by custom file meta_key number
- What functions does WordPress use for filtering / sanitizing comments?
- Archive not sorting correctly
- 4 column-loop ordered alphabetically with entries grouped (and labeled) by their first letter
- Add a custom meta box for client to order CPT posts how they want
- Custom post type sorting: alphabetical order
- get comments by current user inside page template
- WP_Query of custom post type sorted by meta_key has unexpected results
- Enable comments on custom page type – option not showing in “customize” for theme
- Allow anonymous comments just in a custom post type
- Send email messages after comment was submitted
- Post ordering not working with custom field
- Filtered by a custom field, ordered by another
- orderby parameter not working in custom query
- Adding Page Ordering to a Custom Page Type
- comments hooks on custom post type
- delete duplicate meta_value with same post_id
- Get the latest comment from a custom post type where depth = 1?
- Displaying Portfolio Filter by Category Order (alphabetically)
- Changing WordPress sort order for returned child pages
- Why will comments not remain open on custom post types?
- Order posts by taxonomy terms
- wp rest api orderby field in a custom table
- How to change the comments form title based on post type
- How can I sort the results of a REST API response by the title of a connected custom post type?
- How to order taxonomy terms by most recent post?
- Custom post type order by post_title
- order custom post type posts by custom date
- wp delete duplicate entries of custom post types every 15 minutes
- Wp Query sort order from custom MetaBox
- How to sort custom post’s category by id from the theme’s function.php?
- How to create a discussion post when a custom post is created?
- Show first posts with custom field not empty and order all by title
- Orderby modified only for specific post types
- Help ordering custom query by Title, Ascending
- Custom post type updated to support comments, yet comments don’t appear
- shortcode order for event custom post type
- How to display custom post type ordered by a custom field date
- Custom Post Type Order Index Loop
- Ordering custom post type by custom field without a title
- Turn off comments for given CPT checkbox?
- Author profile comments system
- Create custom post order (with custom post type meta)
- Allow admin to determine the order of queries?
- Posts 2 Posts: query connected — orderby problem
- Order custom post type is beign ignored
- How to order different custom post type in category or tag template page?
- add comments custom fields after user is logged in
- Comments are not working on Custom Post Type
- Error 403 when posting comments to a custom post type from a different page
- Order Wp Query by earliest of 3 dates meta query