I'm facing a strange issue displaying all posts of a certain type of custom post sorted by their category. The code below to display them works fine locally but when I view the page on the server it returns an archive page with three posts instead. How is that possible?

This is the code to display the posts:

 <?php $categories = get_terms('categor', 'orderby=count&order=DESC&hide_empty=1');
 foreach( $categories as $category ):?>

<h2><?php echo $category->name; // Prints the cat/taxonomy group title ?></h2>
<?php $posts = get_posts(array(
         'post_type' => 'downloads',
         'taxonomy' => $category->taxonomy,
         'term' => $category->slug,
         'nopaging' => true,

foreach($posts as $post): 
 setup_postdata($post); //enables the_title(), the_content(), etc. without 
specifying a post ID ?>

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<div class="entry-content"> 

<h3><i class="icon-file"></i> <?php the_title(); ?></h3>

  <p><i class="icon-download-alt"></i> <a href="<?php the_field('file'); ?>"   
  target="_blank">Download file</a></br><?php the_field('discription'); ?></p>

<?php echo '<hr/>'; ?>

 </div><!-- .entry-content -->

 </div><!-- #post-## --> 
 <?php endforeach; ?>
  • In code get_terms first parameter is 'categor' or 'category' ? May 23, 2013 at 12:05
  • No typo there, its categor.
    – NielsPilon
    May 23, 2013 at 12:11
  • are you saying an entirely different template file is loading? if that's the case, your issue has nothing to do with the contents of this template file. start by inspecting the global $wp_query and compare them between the two sites.
    – Milo
    May 23, 2013 at 14:15

4 Answers 4


Because it work fine in local, you can give a shot by changing .htaccess file(change permalink form WordPress setting , make sure you have the server write access to change . htaccess file)


Check get_posts()

you code may be

$posts = get_posts(array(
         'post_type' => 'downloads',
         'tax_query' => array(
                'taxonomy' => $category->taxonomy,
                'field' => 'slug',
                'term' => $category->slug
  • Unfortunately that gives the same result; the archive page.
    – NielsPilon
    May 23, 2013 at 12:32
  • I think, i forgot about 'field' => 'slug' in 'tax_query' in get_posts.try it and also check for get_posts() function link for more info given in answer May 23, 2013 at 12:42
  • Same result unfortunately.
    – NielsPilon
    May 23, 2013 at 13:07
  • your code seems to be good, enable WP_DEBUG in config file to see errors.check both foreach loop closes properly.use var_dump to check output types. May 23, 2013 at 13:19

I think the mistake comes from this line

<?php $categories = get_terms('categor', 'orderby=count&order=DESC&hide_empty=1');
     foreach( $categories as $category ):?>

The get_terms('categor') should be your custom post type taxonomy or custom post type category.

I have a taxonomy called "brands" so my code looks like this:

$categories = get_terms('brands', 'orderby=title&order=ASC&hide_empty=1');
     foreach( $categories as $category ): ?>

The code snippet you are using comes from this website - From http://eppand.com/how-to-order-custom-post-types-by-custom-taxonomy-in-wordpress/

Hope that helps anyone else looking for the answer!


Add this 'posts_per_page' => -1 to 'orderby=count&order=DESC&hide_empty=1'

  • Please edit your answer, and add an explanation: why could that solve the problem? Also note that 'posts_per_page' => -1 would fetch all posts and crash the database on large sites.
    – fuxia
    Sep 8, 2018 at 16:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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