1

I need to sort posts using this loop, is an archive page (pc-archive.php) for a CTP named "PC".

Tried with <?php while ( have_posts('order=desc') ) : the_post(); ?>

but no luck... i must be doing something badly.

then I have tried adding this filter, but i dont get it to work...

function my_custom_archive_order( $vars ) {
  if ( !is_admin() && isset($vars['post_type']) && is_post_type_hierarchical($vars['post_type']) ) {
    $vars['orderby'] = 'menu_order';
    $vars['order'] = 'DESC';
  }

  return $vars;
}
add_filter( 'request', 'my_custom_archive_order');

this is the loop:

             <?php while ( have_posts() ) : the_post(); ?>
      <div class="galleryphotos">
    <a target="_blank" href="<?php echo types_render_field("pcurl", array("raw" => "true")); ?>">
              <img  class="pcimagestyle" src="<?php echo types_render_field("pcimage", array("raw" => "true")); ?>">
</a>       

    <div class="icons">            <a target="_blank" href="<?php echo types_render_field("pcurl", array("raw" => "true")); ?>"
                      <ul class="pcitems"> 
        <li><?php the_title(); ?> </li>
              <li>   <?php echo do_shortcode('[types field="pcpubname"][/types]'); ?>  </li>     
              <li>   <?php echo do_shortcode('[types field="pcdate" format="d/m/Y"][/types]'); ?></li>
                </ul>
              </a>
</div>
               </div>       <?php endwhile; // end of the loop. ?>
0

This filter has to be set in functions.php. Your filter won't work as expected because your condition is wrong. The condition here imply that you try to order pages (hierarchical post) instead of regular posts-like entries.

You have to be careful using this filter as it has effect on your whole website.

function my_custom_archive_order( $vars ) {
  if ( !is_admin() && isset($vars['post_type']) && $vars['post_type'] == 'pc' ) {
    $vars['orderby'] = 'menu_order';
    $vars['order'] = 'DESC'; // you probably don't need this because that's the default behaviour
  }

  return $vars;
}
add_filter( 'request', 'my_custom_archive_order');

https://codex.wordpress.org/Plugin_API/Filter_Reference/request

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.