doukanhua0752 2018-01-24 13:14
浏览 138
已采纳

在word_中的get_posts或WP_query函数中的orderby和order过滤器不起作用

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'
                )
            )     
            ));

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 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'
            )
        )
        ));

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..

  • 写回答

2条回答 默认 最新

  • dongtui6347 2018-02-20 08:57
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)