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

WordPress中get_posts/WP_query的orderby与order排序失效求助

Let's break down your issues step by step and figure out why the sorting isn't working as expected—first with get_posts() and then with WP_Query.

1. Why get_posts() sorting wasn't working

get_posts() is a wrapper for WP_Query, but it has a few default behaviors that might trip you up. Here's what could be going wrong:

  • Incorrect orderby value: You used 'post_modified', but WP_Query (which powers get_posts()) prefers the alias 'modified' for sorting by the post_modified field. While 'post_modified' should technically work, switching to 'modified' is more reliable in edge cases.
  • Sticky posts interference: WordPress automatically pushes sticky posts to the top of results, even if you set a custom sort. Adding 'ignore_sticky_posts' => true fixes this.
  • Cache: Old cached results might be sticking around. Clear your site's object cache (e.g., Redis, Memcached) and page cache (e.g., WP Rocket) to rule this out.

Here's a revised get_posts() call that should resolve the sorting issue:

$arrPostDtls = get_posts(array(
    'post_type' => 'kiaarticles',
    'posts_per_page' => -1,
    'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
    'orderby' => 'modified', // Use the WP_Query alias instead of raw field name
    'order' => 'DESC',
    'ignore_sticky_posts' => true, // Prevent sticky posts from breaking sort order
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'products',
            'field' => 'slug',
            'terms' => $arrTermSlug,
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'kiacategory',
            'field' => 'slug',
            'terms' => $arrCTermSlug,
            'operator' => 'IN'
        )
    )
));

2. Why WP_Query SQL works but results don't

You mentioned the generated SQL runs correctly in phpMyAdmin, but looping through $arrPostDtls->posts gives unsorted results. This is tricky, but here are the most likely culprits:

Missing order parameter

Your WP_Query code doesn't include 'order' => 'DESC'! By default, WP_Query uses ascending (ASC) order. Even if your SQL looks correct (maybe you added it later?), explicitly setting this parameter ensures you get the descending order you want.

Cache is serving old results

If your site uses any kind of object caching (like WordPress's built-in transient cache) or page caching, it might be serving stale query results. Clear all caches and test again.

Hooks are modifying results post-query

A theme or plugin could be using hooks like posts_results or posts_orderby to reorder your posts after the SQL runs. To test this:

  • Switch to a default WordPress theme (e.g., Twenty Twenty-Four)
  • Disable all plugins
  • Run your query again. If sorting works now, re-enable plugins/themes one by one to find the culprit.

Previous query residue

If you ran another WP_Query or query_posts before this one without resetting the global state, it might be interfering. Add these lines right before your WP_Query call to reset everything:

wp_reset_query();
wp_reset_postdata();

Sticky posts overriding sort

Just like with get_posts(), sticky posts can break your custom sort. Add 'ignore_sticky_posts' => true to your query parameters to fix this.

Here's the fixed WP_Query code with all these safeguards:

// Reset previous queries to avoid interference
wp_reset_query();
wp_reset_postdata();

$arrPostDtls = new WP_Query(array(
    'post_type' => 'kiaarticles',
    'posts_per_page' => -1,
    'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
    'orderby' => 'modified',
    'order' => 'DESC', // Explicitly set descending order
    'ignore_sticky_posts' => true, // Ignore sticky posts
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'products',
            'field' => 'slug',
            'terms' => $arrTermSlug,
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'kiacategory',
            'field' => 'slug',
            'terms' => $arrCTermSlug,
            'operator' => 'IN'
        )
    )
));

// Verify results by checking post_modified values (for debugging)
foreach ($arrPostDtls->posts as $post) {
    echo $post->post_modified . ' - ' . $post->post_title . '<br>';
}

Final check

If none of this works, dump the actual post_modified values from $arrPostDtls->posts (like in the code above) to confirm the data itself is ordered correctly. Sometimes we assume the sort is wrong, but the database values might not be what we expect!

内容的提问来源于stack exchange,提问作者OM The Eternity

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:25:07