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
- Why are the comments disabled by default on my custom_post_types?
- Displaying a custom post type alphabetically
- Display Authors Comments on Profile Page
- Sort Order for a Custom Query in a Post Type Archive Not Working
- Select All in Parent Category, Group by Child Category?
- Ordering posts having multiple post-meta date fields
- modify all posts of a category to “no comments allowed”
- Order Posts by Taxonomy and Meta Value
- Custom comment status possible?
- feed links for custom post type pages
- Custom Post type sort order not working in the admin area
- Seperating custom post type comments from regular blog posts comments
- Order posts by (hierarchical custom) taxonomy terms and term children
- Compare 3 custom fields and sort by oldest
- Recent comments on author page?
- ascending order custom post type
- query order by date on custom type: wrong order
- Comment moderation on custom post types
- Taxonomy list. Order by a specific custom post type count
- WooCommerce – Show orders for a specific product ? [closed]
- Automatically check “Allow comments” for custom post type [duplicate]
- How could I create a ‘private comments’ section on a custom post type?
- Custom Post Type order by modified date and custom meta field issue
- Custom Post Type Pagination & duplicate posts
- Separate comment section for post type in dashboard
- Count all comments of a custom post type
- Insert data on comment post
- 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
- Rewrite and custom post type: order by custom field not working
- How to set order for a custom post type
- Previous/Next Link by Meta Value in CPT
- Custom Post Type Set Comments ON by default without show METABOX
- How to order separated Custom Post Search results
- WordPress comments_open for specific user role or post type
- How to do WP_Query with two meta fields with orderby clause
- Allow duplicate slugs for custom post type with taxonomies
- How to order and group custom post type posts by year and month
- Create another comment system for CPT
- Display Woocommerce Orders for specific author [closed]
- How to assign classes to all elements?
- custom comments form for custom post type
- Order by custom field meta_key date and then by custom file meta_key number
- comment_notification_text filter for custom post type
- 4 column-loop ordered alphabetically with entries grouped (and labeled) by their first letter
- Ascending Order is not Working in Custom Post Type Listining
- Custom post type sorting: alphabetical order
- get comments by current user inside page template
- $wpdb->get_col and ORDER BY?
- User-submitted reviews of different custom post types
- Check if user has comment on current post
- Having Issue on Ordering CPT by Custom Field In Custom WP Query
- WP_Query of custom post type sorted by meta_key has unexpected results
- WP_Query not resetting after wp_reset_postdata
- Enable comments on custom page type – option not showing in “customize” for theme
- duplicate comment section functionality and call it something else for custom post types
- Filtered by a custom field, ordered by another
- orderby parameter not working in custom query
- custom comment fields on CPT
- unapprove comments on custom post type
- delete duplicate meta_value with same post_id
- Enable comments for post when comments meta box removed
- Get the latest comment from a custom post type where depth = 1?
- show most viewed posts of last days by link?
- How to add comments to my custom post type but hide it from default WordPress Comments section?
- Display Parent-Child Posts in specific order by comparing IDs in array
- 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?
- Comments on posts with new custom post type redirected to another post
- Order posts alphabetically with numbers but some of the posts has numbers in the title
- How to order taxonomy terms by most recent post?
- Orderby if between two meta fields
- SEARCH QUERIES – REVERSE OUTPUT
- order custom post type posts by custom date
- How to order multiple custom post types with same taxonomy by date
- Using Orderby and meta_value to order natural/alphanumerical
- get comments for custom post type
- What’s the most efficient way to get two queries based on an if statement?
- How to create a discussion post when a custom post is created?
- Adding second comments.php for reviews
- WP Query with multiple post types ordered by custom meta date then published date
- Orderby modified only for specific post types
- WPML – Auto Duplicate Post Issue
- Custom post order when using OR relation
- shortcode order for event custom post type
- Comments not enabled for custom post types
- Order Custom Posts by Several Fields
- show previous/next post orderer by title in custom post types
- Custom post type: can’t enable comments by default
- Comment moderation with disqus comments
- 404 on Pages for Custom Post Type & Query_Posts
- Allow admin to determine the order of queries?
- Order custom post type is beign ignored
- How to order different custom post type in category or tag template page?
- WP_query orderby not working after WP4.0 update [closed]
- ACF Field to set Publish Date – Post Duplication upon Update
- Query order by meta value force specific tag first
- Comments not working (error not allowed) on posts with custom post status
- Query multiple post types – Order by type and post title title