Ordering by publishing date

Ordering by publishing date

brancosousabrancosousa Posts: 3Questions: 1Answers: 0

Hello I am having problems with a table ...
I am using DataTable to display / list post types data from a site in WordPres.
All of them, so far, were working 100%. But the last one has been giving a headache. I can't display the posts in order of publication, like the previous ones.

To sum it all up, I don't know how to sort by the date the posts are posted.

<?php
$cols = 'lic-col0|lic-col1|lic-col2|lic-col3|lic-col4';
$cols = explode("|", $cols);
$colheads = explode("|", $colheads);
foreach ($cols as $index => &$col){
   $col = explode(",", $col);
   $colheads = !isset($colheads[$index])?$col[0]:$colheads[$index];
}; ?>
<!--links estilo e script -->
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/table_style.css" type='text/css' media='all' />
<!--link rel="stylesheet" href="https://cdn.datatables.net/plug-ins/1.10.19/integration/font-awesome/dataTables.fontAwesome.css" type='text/css' media='all' /-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="<?php bloginfo('template_directory'); ?>/DataTables/jQuery-1.12.4/jquery-1.12.4.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/DataTables/DataTables-1.10.18/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min.js"></script>

<!-- Script tabela -->  
<div class="container"> 
    <div> 
        <table style="width: 100%; margin: 0 auto 2em auto;" cellspacing="0" cellpadding="1" border="0">
        <thead>
            <tr>
                <th>Pesquisa</th>
                <th>Dado para pesquisa</th>
                
            </tr>
        </thead>
        <tbody>
            <tr id="filter_global">
                <td>Pesquisa geral</td>
                <td align="center"><input type="text" class="global_filter" id="global_filter"></td>
             </tr>
            
            
            <tr id="filter_col0" data-column="0">
                <td>Data Inicial</td>
                <td align="center"><input type="text" class="column_filter" id="col0_filter"></td>
                
            </tr>

            <tr id="filter_cols1" data-column="1">
                <td>Secretaria</td>
                <td align="center"><input type="text" class="column_filter" id="col1_filter"></td>
            
            </tr>

            <tr id="filter_col2" data-column="2">
                <td>Exercício</td>
                <td align="center"><input type="text" class="column_filter" id="col2_filter"></td>
                
            </tr> 

            <tr id="filter_col3" data-column="3">
                <td>Objeto ou valor</td>
                <td align="center"><input type="text" class="column_filter" id="col3_filter"></td>
                
            </tr> 
            
            </tbody>
        </table> 
                        </div>
       <div class="row">

        <table id="tabela-licitacao"  data-order='[[0, "DESC" ]]' data-page-length='10'>
            <thead>
            <tr>
                <th style="width: 200px">Número da Licitação<br>Modalidade</th>
                <th>Publicação<br>Objeto</th>
                <th>Abertura</th>
                <th>Situação</th>
                <th>Detalhes</th>
            </tr>
            </thead>
           
            <?php query_posts('post_type=licita&post_per_page=-1&order=DESC'); ?>

<tbody><?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<tr>
                        <?php
                        $tcols = $cols;
                        array_shift($tcols);
                        foreach ($cols as $colx => $cold){ ?>
                          
                        <td>
                            <?php 
                            $sobre_grupo = get_field('sobre');
                            $abertura_grupo = get_field('abertura');
                            $publicacao_grupo = get_field('publicacao');
                            $responsaveis_grupo = get_field('responsaveis');

                            foreach ($cold as $cx => $c){
                                $cxc = ($cx > 0)?'|':'|';
                                switch ($c) {
                                    case 'lic-col0':
                                           
                                        echo '<strong>'; echo get_the_title(); echo '</strong>';
                                        echo '</br>';
                                        echo $sobre_grupo['modalidade'];
                                        
                                        break;

                                    case 'lic-col1':
                                        echo '<strong>';echo $sobre_grupo['publicacao_do_aviso'];echo '</strong>';
                                        echo '</br>';
                                        echo the_field("objeto") ; 
                                        break;  

                                    case 'lic-col2':
                                        echo '<strong>';echo $abertura_grupo['data_de_abertura'];echo '</strong>';
                                        break;
                                   
                                    case 'lic-col3':
                                        
                                        echo $sobre_grupo['situacao'];                                    
                                        break;
                                        
                                    case 'lic-col4' :
                                        echo "<a class=\"botao\" href='".get_the_permalink()."' role=\"button\"> Visualizar </a>" ;  
                                        break;

                            }}
                            
                                        ?>


                        </td> 
                        <?php } endwhile; ?>

                    </tr>
                    
            </tbody> 
            
        </table>
        <script>
$(document).ready(function() {
    $('#tabela-licitacao').DataTable( {
        "language": {
    "sEmptyTable": "Nenhum registro encontrado",
    "sInfo": "Mostrando de _START_ até _END_ de _TOTAL_ registros",
    "sInfoEmpty": "Mostrando 0 até 0 de 0 registros",
    "sInfoFiltered": "(Filtrados de _MAX_ registros)",
    "sInfoPostFix": "",
    "sInfoThousands": ".",
    "sLengthMenu": "_MENU_ resultados por página",
    "sLoadingRecords": "Carregando...",
    "sProcessing": "Processando...",
    "sZeroRecords": "Nenhum registro encontrado",
    "sSearch": "Pesquisar",
    "oPaginate": {
        "sNext": "Próximo",
        "sPrevious": "Anterior",
        "sFirst": "Primeiro",
        "sLast": "Último"
    },
    "oAria": {
        "sSortAscending": ": Ordenar colunas de forma ascendente",
        "sSortDescending": ": Ordenar colunas de forma descendente"
    }
}, dom: '<lB<rt>ip>', 
        buttons:  [ 
{ extend: 'copy', text: '<i class="fa fa-copy"></i> COPIAR' },
{ extend: 'print', text: '<i class="fa fa-print"></i> IMPRIMIR' },
{ extend: 'pdf', text: '<i class="fa fa-file-pdf-o"></i> PDF' },
{ extend: 'csv', text: '<i class="fa fa-file-text-o"></i> CSV'},
{ extend: 'excel', text: '<i class="fa fa-file-excel-o"></i> EXCEL' },
],

 select: false
    } ); 
} );
function filterGlobal () {
    $('#tabela-licitacao').DataTable().search(
        $('#global_filter').val()
        
    ).draw();
}
 
function filterColumn ( i ) {
    $('#tabela-licitacao').DataTable().column( i ).search(
        $('#col'+i+'_filter').val()
       
    ).draw();
}
 
$(document).ready(function() {
    $('#tabela-licitacao').DataTable();
 
    $('input.global_filter').on( 'keyup click', function () {
        filterGlobal();
    } );
 
    $('input.column_filter').on( 'keyup click', function () {
        filterColumn( $(this).parents('tr').attr('data-column') );
    } );
} );
</script>

<?php  endif; // Loop ?> 
<?php wp_reset_query(); ?>

</div> 
</div>

Replies

  • kthorngrenkthorngren Posts: 20,276Questions: 26Answers: 4,765

    This blog presents the recommended way to handle date/time sorting.

    Kevin

  • brancosousabrancosousa Posts: 3Questions: 1Answers: 0

    I found a solution...
    I put a line on the script
    ...
    sorting=false
    ...

This discussion has been closed.