You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装Yoast SEO插件,如何将文章归档页按社交分享数排序?

Sort Archive Posts by Yoast SEO Social Share Count

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_indexable table.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:07:30