Sort by custom Fields and by publication date

Asked

Viewed 84 times

0

My problem is this, I have a post_type in the name of "collections" and within it several posts with custom fields "situation of the piece" and "collection of the piece". 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 ordered by publication date. In Firefox this order is right, but in Chrome and IE, the order by date (wordpress default, by the way), does not work. I know it’s the javascript that might be cluttering them, but I don’t know how to fix it... Follow the link that this is happening: https://malumacedo.com.br/marca/basico-brasil/

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

1 answer

0


You can make this ordination 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 the ordering in the link below. Documentation

You said you could order by custom Fields with javascript and that works perfectly, but if you want to do this ordering by Wordpress just take a look at this documentation.

  • Had already tried this, this command does not work either, in Google Chrome or IE. They should be ordered as it is in Firefox (which is correct by the order of posting), this is the link https://malumacedo.com.br/todas-collections/ . It’s like they sort randomly and just follow the same javascript command. I’m studying this ordering by custom Fields, to see if I can find a way. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.