已安装Yoast SEO插件,如何将文章归档页按社交分享数排序?
Absolutely! You can absolutely sort your archive posts by Yoast SEO's social share count—your current loop uses the default main query, so we just need to tweak that query to prioritize share numbers instead of the default date order. Let's walk through the most reliable way to do this.
Background: Where Yoast Stores Social Shares
Yoast SEO now stores aggregated social share counts in its dedicated wp_yoast_indexable database table (this is more efficient than old postmeta storage). The social_shares column in this table holds the total share count for each post, which is perfect for sorting.
Step 1: Add a Custom Query Filter
Add this code to your theme's functions.php file (or use a code snippet plugin like Code Snippets to avoid modifying theme files directly):
function sort_archives_by_yoast_social_shares( $query ) { global $wpdb; // Target only front-end archive main queries (skip admin and custom loops) if ( ! is_admin() && $query->is_main_query() && ( is_archive() || is_category() || is_tag() ) ) { // Join Yoast's indexable table to access share counts $query->set( 'join', "LEFT JOIN {$wpdb->prefix}yoast_indexable ON {$wpdb->prefix}posts.ID = {$wpdb->prefix}yoast_indexable.object_id" ); // Sort by share count (descending), then by post date if counts are equal $query->set( 'orderby', "{$wpdb->prefix}yoast_indexable.social_shares DESC, {$wpdb->prefix}posts.post_date DESC" ); // Optional: Restrict to posts only (remove if you need custom post types included) $query->set( 'post_type', 'post' ); } } add_action( 'pre_get_posts', 'sort_archives_by_yoast_social_shares' );
Step 2: Verify and Troubleshoot
- No changes to your loop needed: Your existing
while ( have_posts() ) : the_post();loop will automatically use the modified sorted query. - Clear caches: Yoast caches share data, and your site might have page caching enabled. Clear both to see the updated order.
- Reindex Yoast data: If share counts seem outdated, go to Yoast SEO → Tools → SEO Data Optimization and run the optimization to refresh the
wp_yoast_indexabletable. - Legacy Yoast versions: If you're on an older Yoast version that uses postmeta instead, replace the above code with this postmeta-based approach (note: this targets total shares from the serialized meta value):
function sort_archives_by_yoast_postmeta_shares( $query ) { if ( ! is_admin() && $query->is_main_query() && ( is_archive() || is_category() || is_tag() ) ) { $query->set( 'meta_key', '_yoast_wpseo_social_shares' ); $query->set( 'orderby', 'meta_value' ); $query->set( 'order', 'DESC' ); // Include posts with no share data at the end $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => '_yoast_wpseo_social_shares', 'compare' => 'EXISTS' ), array( 'key' => '_yoast_wpseo_social_shares', 'compare' => 'NOT EXISTS' ) ) ); } } add_action( 'pre_get_posts', 'sort_archives_by_yoast_postmeta_shares' );
This should get your archive pages sorting posts by their social share count exactly as you want!
内容的提问来源于stack exchange,提问作者Polly McDoogle

