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..
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:
And My results were displayed as I was willing to
Try
Update after you update the question