Display list of popular posts sorted by share count

Shared Counts stores the total share count as post metadata using the shared_counts_total key. This lets you easily query for popular content

Query with code

To query for a list of your posts with the highest share count:

$loop = new WP_Query( array(
	'orderby' => 'meta_value_num',
	'meta_key' => 'shared_counts_total',
) );

You can also combine this with more advanced queries. Here’s a list of posts published in the last month, sorted by share count.

$loop = new WP_Query( array( 
	'orderby' => 'meta_value_num',
	'meta_key' => 'shared_counts_total',
	'date_query' => array(
		array(
			'after' => '3 months ago',
		)
	)
) );

I often use this approach for “Popular Posts” section so the listings change over time and don’t always show a really popular article from 5 years ago.

Query with shortcode

I recommend you try my Display Posts plugin for dynamically listing content without any code. There are many parameters for customizing the query and its display.

To display a list of posts with the highest share count (copying the first code sample above):

[display-posts orderby="meta_value_num" meta_key="shared_counts_total"]

To display a list of posts published in the last month, sorted by share count (copying the second code sample above):

[display-posts orderby="meta_value_num" meta_key="shared_counts_total" date_query_after="1 month ago"]

Published by Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributor to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.