orderby and order filter in get_posts or WP_query

2019-07-26 20:29发布

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

标签: php wordpress
2条回答
走好不送
2楼-- · 2019-07-26 21:16

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

查看更多
孤傲高冷的网名
3楼-- · 2019-07-26 21:18

Try

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

Update after you update the question

$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'
            )
        )
        ));
查看更多
登录 后发表回答