简体   繁体   中英

orderby and order filter in get_posts or WP_query function in wordpress not working

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM