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
- Why are the comments disabled by default on my custom_post_types?
- Why are comments and trackbacks still getting through for custom post types?
- Displaying a custom post type alphabetically
- Order by & include array by specific post ids
- Display Authors Comments on Profile Page
- List all posts commented by current user
- 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
- 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)
- 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
- Add comments meta fields to comments metabox on post edit screen
- Order posts by (hierarchical custom) taxonomy terms and term children
- Exclude post_type from admin comments_list
- Compare 3 custom fields and sort by oldest
- List upcoming events, ordered by date in a custom field
- Recent comments on author page?
- 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
- 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]
- Re-order posts in query after
- How could I create a ‘private comments’ section on a custom post type?
- 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 order by modified date and custom meta field issue
- Custom Post Type Pagination & duplicate posts
- Custom Post Type loops and Disqus
- How to query different post types in specific order?
- “Allow Comments” box cannot be checked
- 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?
- custom comments on specific post type
- 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
- Rewrite and custom post type: order by custom field not working
- get_comments not working on custom post types
- 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
- Wp_query: sort by PHP variable
- Allow user to set custom order to a list of custom taxonomies?
- Using Same Slug With Multiple Post Types
- Sort CPT archive by order prevents sorting in admin
- 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
- 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
- Create another comment system for CPT
- How can the recipients and content for comment email notifications on a custom post type be changed?
- 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
- Comments not enabled for custom post types
- Order Custom Posts by Several Fields
- Custom Post Type Order Index Loop
- show previous/next post orderer by title in custom post types
- Ordering custom post type by custom field without a title
- Turn off comments for given CPT checkbox?
- Author profile comments system
- Custom post type: can’t enable comments by default
- Create custom post order (with custom post type meta)
- Comment moderation with disqus comments
- 404 on Pages for Custom Post Type & Query_Posts
- 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?
- WP_query orderby not working after WP4.0 update [closed]
- add comments custom fields after user is logged in
- ACF Field to set Publish Date – Post Duplication upon Update
- Comments are not working on Custom Post Type
- Error 403 when posting comments to a custom post type from a different page
- Query order by meta value force specific tag first
- Order Wp Query by earliest of 3 dates meta query
- Comments not working (error not allowed) on posts with custom post status
- Query multiple post types – Order by type and post title title
- Admin custom post ordering