Home Iterating the posts of wp_query gives me different order as what I am getting through the query or post's array itself
Reply: 1

Iterating the posts of wp_query gives me different order as what I am getting through the query or post's array itself

OM The Eternity
1#
OM The Eternity Published in 2018-02-14 09:29:21Z

This question already has an answer here:

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

I have implemented a WP_query function to get the data in wordpress. I iterate the available data ordered by 'modified', but when it is getting displayed on the screen, I get it ordered by alphabets rather than 'post_modified' field, I checked the query available in the results as well, I cannot see any other order by other than 'post_modified', below is the 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' => 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'
            )
        )
        ));

 $strData = '
                <table id="id_'.$intPostId.'" class="table table-striped table-bordered" cellspacing="0" width="100%">
                <thead>
                    <tr>
                        <th>Title</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th>Title</th>                
                    </tr>
                </tfoot><tbody>';
             foreach ($arrPostDtls->posts as $arrPostDtlsVal) {

                $intResultpostId = $arrPostDtlsVal->ID;
                $strPostLink = get_permalink($intResultpostId);
                $strPostTitle = $arrPostDtlsVal->post_title;
                $strData.='<tr><td><a href = "' . $strPostLink . '">' . $strPostTitle . '</a></td></tr>';

            }
            $strData .='</tbody>
            </table>';


        echo $strData;

The result displayed is as below:

But we are expecting this order as per post_modified field of DB table column.

Below is the query I get in $arrPostDtls->request

SELECT ap_ac_posts.* FROM ap_ac_posts  INNER JOIN ap_ac_term_relationships ON (ap_ac_posts.ID = ap_ac_term_relationships.object_id)  INNER JOIN ap_ac_term_relationships AS tt1 ON (ap_ac_posts.ID = tt1.object_id) WHERE 1=1  AND ( 
  ap_ac_term_relationships.term_taxonomy_id IN (40719,40935,40945,41762,41763,46525,46729,47308,49224,49377,49378,49379,49380,49381,49383,49649,49650) 
  AND 
  tt1.term_taxonomy_id IN (40693,40956,41114)
) AND ap_ac_posts.post_type = 'kiaarticles' AND ((ap_ac_posts.post_status = 'publish' OR ap_ac_posts.post_status = 'draft' OR ap_ac_posts.post_status = 'pending' OR ap_ac_posts.post_status = 'trash' OR ap_ac_posts.post_status = 'auto-draft')) GROUP BY ap_ac_posts.ID ORDER BY ap_ac_posts.post_modified DESC 

Please suggest what and where could be this order is being updated?

Shahid Latif
2#
Shahid Latif Reply to 2018-02-14 10:13:50Z

Use orderby and order parameters separately in wp_quer arguments like

$args = array(
'orderby' => 'modified',
'order'   => 'DESC',

); $query = new WP_Query( $args );

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300437 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO