-1

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:

    $arrProducts = get_the_terms($intPostId, 'products');

    if (is_array($arrProducts) && count($arrProducts)) {
        foreach ($arrProducts as $intKey => $arrProductsObj) {
            $intTermId = $arrProductsObj->term_id;
            $strSlug = $arrProductsObj->slug;
            $strName = $arrProductsObj->name;
            $children = get_term_children($intTermId, 'products'); // get children
            if(sizeof($children)==0)
            {
                array_push($arrTermId, $intTermId);
                array_push($arrTermName, $strName);
                array_push($arrTermSlug, $strSlug);
            }

        }
    }

    $intCTermId = "";
    $arrCTermId = array();
    $arrCTermName = array();
    $arrCTermSlug = array();
    $arrCProducts = get_the_terms($intPostId, 'kiacategory');
         /* echo "<pre>";
  print_r($arrCProducts);exit(); */ 
    if (is_array($arrCProducts) && count($arrCProducts)) {   
        foreach ($arrCProducts as $intCKey => $arrCProductsObj) {
            $intCTermId = $arrCProductsObj->term_id;
            $strCSlug = $arrCProductsObj->slug;
            $strCName = $arrCProductsObj->name;
            $children = get_term_children($intCTermId, 'kiacategory'); // get children
            if(sizeof($children)==0)
            {
                array_push($arrCTermId, $intCTermId);
               array_push($arrCTermName, $strCName);
               array_push($arrCTermSlug, $strCSlug);
            }

        }
    }


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

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

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

  • are you getting the correct results in the wrong order or are the results also wrong? – inarilo Feb 13 '18 at 8:40
  • I am getting results in a alphabetic order of post_title, which I dont see applied anywhere in the code above, and hence it looks like my orderby post_modified is not working at all – OM The Eternity Feb 13 '18 at 8:42
  • 1
    it should be just 'modified' not 'post_modified'. Can also skip 'order' since it's 'DESC' by default. – inarilo Feb 13 '18 at 8:55
  • @inarilo I tried that as well, but it didnt work either. – OM The Eternity Feb 13 '18 at 8:56
  • uh just realised you are not assigning your get_posts result to anything. you are probably just printing out the results of the default query, not yours. – inarilo Feb 13 '18 at 11:41
0

I found my problem on the UI side (thanks to @CBroe) 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.