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

时间:2018-01-24 13:14:26

标签: php wordpress

我在wordpress插件中有功能,使用get_posts($ array)查询帖子。但是我希望这可以通过DESCENDING命令的post表的post_modified字段来命令,我的代码如下:

     $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子句来相应地进行排序,但它不起作用。请建议或帮助我按照我的意愿进行分类。

UPDATE 为了获得其他方式,我使用了WP_query方法来获取帖子。我在下面的代码中实现了:

$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->帖子”时,它仍然给了我旧的结果..请说明这里有什么问题..

2 个答案:

答案 0 :(得分:1)

尝试

 get_posts(array(
        'post_type' => 'kiaarticles',
        'posts_per_page' => -1, 
        'post_status' => array('publish', 'pending', 
        'trash','draft','auto-draft') ,
        'orderby' => array('post_modified' => 'DESC'),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'products',
                'field' => 'slug',
                'terms' => $arrTermSlug,
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'kiacategory',
                'field' => 'slug',
                'terms' => $arrCTermSlug,
                'operator' => 'IN'
            )
        )     
        ));
更新问题后

更新

$arrPostDtls = new WP_query(array(
        'post_type' => 'kiaarticles',
        'posts_per_page' => -1, //unlikely high
        'post_status' => array('publish', 'pending', 'trash','draft','auto-draft'),
        'orderby' => array('modified' => 'DESC'),
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'products',
                'field' => 'slug',
                'terms' => $arrTermSlug,
                'operator' => 'IN'
            ),
            array(
                'taxonomy' => 'kiacategory',
                'field' => 'slug',
                'terms' => $arrCTermSlug,
                'operator' => 'IN'
            )
        )
        ));

答案 1 :(得分:-1)

我在UI端发现了我的问题(感谢@Zhilevan),其中ajax响应被暴露给jQuery库DataTable(),其默认排序按字母顺序排序。我将排序参数设置为false为:

$("#someid").DataTable({"ordering":false});

我的结果显示为我愿意