3 replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
source | link

I'm afraid orderby taxonomy terms post count is not possible by WP_Query. See this answerSee this answer. So here I've written a SQL based function for you. It'll order your posts based on terms post count.

I'm afraid orderby taxonomy terms post count is not possible by WP_Query. See this answer. So here I've written a SQL based function for you. It'll order your posts based on terms post count.

I'm afraid orderby taxonomy terms post count is not possible by WP_Query. See this answer. So here I've written a SQL based function for you. It'll order your posts based on terms post count.

2 added 1274 characters in body
source | link

It'll return you all the data from you posts table with post_tag term name and this terms post count. But it has a simple limitation and that is if you have one tag in multiple posts then the post will appear multiple time for each post_tag term it is attached to. You can use GROUP BY posts.ID to remove the duplicate posts, but it'll not give you all the terms and terms count. So with GROUP BY posts.ID the function will be like below-

function the_dramatist_order_posts_by_tag_count( $args = '' ) {
    global $wpdb;
    $param = empty($args) ? 'post_tag' : $args;
    $sql = $wpdb->prepare("SELECT posts.*, terms.name AS tag, count 
                            FROM {$wpdb->posts} AS posts
                                 LEFT JOIN {$wpdb->term_relationships} AS t_rel
                                            ON (t_rel.object_id = posts.ID)
                                 LEFT JOIN {$wpdb->term_taxonomy} AS t_tax
                                            ON (t_rel.term_taxonomy_id = t_tax.term_taxonomy_id)
                                 LEFT JOIN {$wpdb->terms} AS terms
                                            ON (terms.term_id = t_tax.term_id)
                            WHERE taxonomy LIKE '%s'
                            GROUP BY posts.ID
                            ORDER BY t_tax.count DESC, terms.name",
        array( $param )
    );
    return $wpdb->get_results($sql);
}

Use which suits your need better.

It'll return you all the data from you posts table with post_tag term name and this terms post count. But it has a simple limitation and that is if you have one tag in multiple posts then the post will appear multiple time for each post_tag term it is attached to.

It'll return you all the data from you posts table with post_tag term name and this terms post count. But it has a simple limitation and that is if you have one tag in multiple posts then the post will appear multiple time for each post_tag term it is attached to. You can use GROUP BY posts.ID to remove the duplicate posts, but it'll not give you all the terms and terms count. So with GROUP BY posts.ID the function will be like below-

function the_dramatist_order_posts_by_tag_count( $args = '' ) {
    global $wpdb;
    $param = empty($args) ? 'post_tag' : $args;
    $sql = $wpdb->prepare("SELECT posts.*, terms.name AS tag, count 
                            FROM {$wpdb->posts} AS posts
                                 LEFT JOIN {$wpdb->term_relationships} AS t_rel
                                            ON (t_rel.object_id = posts.ID)
                                 LEFT JOIN {$wpdb->term_taxonomy} AS t_tax
                                            ON (t_rel.term_taxonomy_id = t_tax.term_taxonomy_id)
                                 LEFT JOIN {$wpdb->terms} AS terms
                                            ON (terms.term_id = t_tax.term_id)
                            WHERE taxonomy LIKE '%s'
                            GROUP BY posts.ID
                            ORDER BY t_tax.count DESC, terms.name",
        array( $param )
    );
    return $wpdb->get_results($sql);
}

Use which suits your need better.

    Post Undeleted by CodeMascot
    Post Deleted by CodeMascot
1
source | link

I'm afraid orderby taxonomy terms post count is not possible by WP_Query. See this answer. So here I've written a SQL based function for you. It'll order your posts based on terms post count.

function the_dramatist_order_posts_by_tag_count( $args = '' ) {
    global $wpdb;
    $param = empty($args) ? 'post_tag' : $args;
    $sql = $wpdb->prepare("SELECT posts.*, terms.name AS tag, count 
                            FROM {$wpdb->posts} AS posts
                                 LEFT JOIN {$wpdb->term_relationships} AS t_rel
                                            ON (t_rel.object_id = posts.ID)
                                 LEFT JOIN {$wpdb->term_taxonomy} AS t_tax
                                            ON (t_rel.term_taxonomy_id = t_tax.term_taxonomy_id)
                                 LEFT JOIN {$wpdb->terms} AS terms
                                            ON (terms.term_id = t_tax.term_id)
                            WHERE taxonomy LIKE '%s'
                            ORDER BY t_tax.count DESC, terms.name",
        array( $param )
    );

    return $wpdb->get_results($sql);
}

It'll return you all the data from you posts table with post_tag term name and this terms post count. But it has a simple limitation and that is if you have one tag in multiple posts then the post will appear multiple time for each post_tag term it is attached to.

This function works on post_tag taxonomy by default. If you want to use it with other taxonomy just pass it as parameter like the_dramatist_order_posts_by_tag_count( 'category' )

Better you do a print_r on this function before using it in production to understand the data structure.

Hope that helps.