根据最高浏览量对 WordPress 帖子进行排序

问题描述 投票:0回答:1

这是我的帖子,仅显示那些有观点的帖子。视图使用 wp-view-counter 插件进行计数,并显示这些帖子以及被分类为趋势术语的人,无论他们是否有视图。现在我唯一想要实现的就是按最高浏览次数到最低浏览次数显示帖子。

function my_custom_elementor_query( $query ) {
    // Check if it's the Elementor post query with a specific query ID
        global $wpdb;

        // Construct the SQL query
        $sql = "SELECT DISTINCT {$wpdb->posts}.ID FROM {$wpdb->posts}";

        // Add JOIN clauses for meta and taxonomy tables
        $sql .= " LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)";
        $sql .= " LEFT JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)";
        $sql .= " LEFT JOIN {$wpdb->term_taxonomy} ON ({$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id)";
        $sql .= " LEFT JOIN {$wpdb->terms} ON ({$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id)";

        // Add WHERE conditions
        $sql .= " WHERE 1=1";
        $sql .= " AND ({$wpdb->postmeta}.meta_key = 'entry_views' AND {$wpdb->postmeta}.meta_value != '')";
        $sql .= " OR ({$wpdb->terms}.slug = 'trending' AND {$wpdb->term_taxonomy}.taxonomy = 'filter')";
        $sql .= " OR ({$wpdb->postmeta}.meta_key = 'entry_views' AND {$wpdb->postmeta}.meta_value != '' AND {$wpdb->terms}.slug != 'trending')";

        // Execute the query
        $post_ids = $wpdb->get_col( $sql );

        // Set post__in parameter to fetch only these posts
        $query->set( 'post__in', $post_ids );
    }
add_action( 'elementor/query/custom_filter', 'my_custom_elementor_query' );
php sql wordpress action
1个回答
0
投票

将此行添加到“$query->set( 'post__in', $post_ids );”之后

$query->set( 'orderby', 'post__in' );

它的工作!

© www.soinside.com 2019 - 2024. All rights reserved.