Sort by custom fields and by date of publication

0

My problem is this, I have a post_type in the name of "collections" and inside it multiple posts with custom fields "situation-of-freck" and "collection of freckles". The result of these two fields are numbers, to make it easier to sort. I got help to sort these custom fields through a javascript, as in the code below, and it works perfectly. However, I also need that within this order the posts are also sorted by date of publication. In Firefox this order is correct, but in Chrome and IE, the order by date (default wordpress, by the way), does not work. I know it's the javascript that may be messing them up, but I do not know how to fix ... Here is the link that is happening: link

<ol id="pecas">
<?php $args = array( 'post_type' => 'colecoes'); 
$wp_query = new WP_Query($args); 
if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<li class="foto" data-char="<?php if( get_field('situacao-da-peca') == '1' ):?>c<?php the_field('colecao-da-peca');?><?php endif;?><?php if( get_field('situacao-da-peca') == '2' ):?>b<?php the_field('colecao-da-peca');?><?php endif;?><?php if( get_field('situacao-da-peca') == '3' ):?>a<?php the_field('colecao-da-peca');?><?php endif;?>">
<div class="imagem"></div>
</li>
<?php endwhile; endif;?>
</ol>
<script>
$("#pecas li").sort(sort_li).appendTo('#pecas');
function sort_li(a, b){
	var r = /[a-z]/; 
   	var aD = $(a).data('char');
   	var bD = $(b).data('char');
   	var va = aD.charCodeAt(0);
  	var vb = bD.charCodeAt(0);
   if(va == vb){ 
      var aN = parseInt(aD.replace(r, ''));
      var bN = parseInt(bD.replace(r, ''));
      return bN > aN ? 1 : -1;
   }

   return vb > va ? 1 : -1;
}
</script>
    
asked by anonymous 20.09.2018 / 22:01

1 answer

0

You can do this sort on Wordpress as follows:

$args  = array( 'post_type'=>'colecoes', 'orderby'=>'date', 'order'=>'DESC' ); // order pode ser ASC ou DESC
$query = new WP_Query( $args ); 

More information about which fields you can use in ordering at the link below. Documentation

You said you were able to sort by custom fields with javascript and it works perfectly, but if you want to do this sort by Wordpress take a look at this documentation .

    
21.09.2018 / 13:53