wordpress 中 get_posts 或 WP_query 函数中的 orderby 和 order 过滤器不起作用 [英] orderby and order filter in get_posts or WP_query function in wordpress not working
问题描述
我在 wordpress 插件中有函数使用 get_posts($array) 查询帖子.但我希望它以降序顺序通过帖子表的 post_modified 字段进行排序,为此我有以下代码:
I have function in wordpress plugin which queries the posts using get_posts($array). But I wanted this to orderby post_modified field of the posts table in DESCENDING order, for which I have this code below:
$arrPostDtls = get_posts(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1,
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft') ,
'orderby' => 'post_modified',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
在这里,我确实实现了 orderby 或 order 子句以对其进行相应排序,但它不起作用.请建议或帮助我按照我的意愿进行排序.
Here, I did implemented the orderby or order clauses to get it sorted accordingly, but it doesnt work. Please suggest or help me to get the sorting as I am willing to.
UPDATE 为了以其他方式获取信息,我使用了 WP_query 方法来获取帖子.为此,我实现了以下代码:
UPDATE To get the things other way, I used the WP_query method to get the posts. for which I implemented below code:
$arrPostDtls = new WP_query(array(
'post_type' => 'kiaarticles',
'posts_per_page' => -1, //unlikely high
'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
'orderby' => 'modified',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'products',
'field' => 'slug',
'terms' => $arrTermSlug,
'operator' => 'IN'
),
array(
'taxonomy' => 'kiacategory',
'field' => 'slug',
'terms' => $arrCTermSlug,
'operator' => 'IN'
)
)
));
由此我收到了包含 SQL 查询的结果,并在 PHPmyadmin 中执行 SQL 查询,我找到了预期的结果,但是当我迭代$arrPostDtls->posts"时,它仍然给我旧的结果.. 请建议这里有什么问题..
From this I recieved the result which also contains the SQL query, and executing the SQL query in PHPmyadmin, I found the exepected result, but when i iterated the "$arrPostDtls->posts", it still gives me the old results.. Please suggest what is wrong here..
推荐答案
我在 UI 方面发现了我的问题(感谢 @Zhilevan),其中 ajax 响应暴露给 jQuery 库 DataTable(),其默认顺序是按字母顺序排序.我将排序参数设置为 false 为:
I found my problem on the UI side (thanks to @Zhilevan) where by ajax response was being exposed to a jQuery library DataTable(), whose default ordering was sorting in alphabetical order. I set the ordering parameter to false as:
$("#someid").DataTable({"ordering":false});
我的结果按照我的意愿显示
And My results were displayed as I was willing to
这篇关于wordpress 中 get_posts 或 WP_query 函数中的 orderby 和 order 过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!