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

WordPress中WP_Query使用Order by title排序无效问题求助

解决WP_Query按标题排序无效的问题

你的问题核心是排序参数位置错误:order和orderby被放在了tax_query的子数组里——这两个参数在tax_query中是用来排序分类术语的,不是排序文章的。要实现文章按标题升序,必须把这两个参数放到WP_Query的顶级参数数组中。

修正后的完整代码如下:

<ul>
    <?php
    $current_category = get_queried_object_id();
    $query = new WP_Query( array(
        'post_type' => 'table_partner', // 自定义文章类型
        'tax_query' => array(
            array(
                'taxonomy' => 'table_partner_tax', // 分类法名称
                'field'    => 'term_id',
                'terms'    => $current_category,
            )
        ),
        'orderby' => 'title', // 移至WP_Query顶级参数
        'order'   => 'ASC'    // 移至WP_Query顶级参数
    ) );
    while ( $query->have_posts() ) : $query->the_post();
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;
    wp_reset_postdata(); // 推荐用wp_reset_postdata()替代wp_reset_query(),后者专为query_posts设计
    ?>
</ul>

你之前尝试的posts_orderby过滤器无效,是因为它仅清空了排序规则,没有设置正确的按标题排序逻辑,且核心的参数位置错误问题未被解决,所以无法生效。

内容的提问来源于stack exchange,提问作者KBKdeveloper

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 21:52:11