0

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; ?>
3
  • In code get_terms first parameter is 'categor' or 'category' ? – Ravinder Kumar May 23 '13 at 12:05
  • No typo there, its categor. – NielsPilon May 23 '13 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 '13 at 14:15
1

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)

0

Check get_posts()

you code may be

$posts = get_posts(array(
         'post_type' => 'downloads',
         'tax_query' => array(
              array(
                'taxonomy' => $category->taxonomy,
                'field' => 'slug',
                'term' => $category->slug
                )
         )
      ));
4
  • Unfortunately that gives the same result; the archive page. – NielsPilon May 23 '13 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 – Ravinder Kumar May 23 '13 at 12:42
  • Same result unfortunately. – NielsPilon May 23 '13 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. – Ravinder Kumar May 23 '13 at 13:19
0

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!

-1

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

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 '18 at 16:58

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.