• Hello,

    I have tried a couple of plugins (Posts per cat & Recent Posts From Each Category) and a number of code snippets i have both written myself and seen in various forums but none do exactly what i need. The closest i have so far is here…

    <?php
    // Lists category IDs we want on the homepage
    $cat_args=array(
      'include' => '6,14,7,4231,30,4405,2346,11,3373,4344,4358,3453,31,10,4308,4232,3452',
       );
    $categories=get_categories($cat_args);
    
    // Loop through each category and get the most recent post
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1,
          'orderby' => 'post_date',
          'order' => 'DESC'
        );
        $posts=get_posts($args);
    
    // Loop through each post and display the data
          if ($posts) {
          foreach($posts as $post) {
    	  setup_postdata($post);
     ?>
    	<div class="panel panel-default panel-show-box panel-<?php
    $category = get_the_category();
    echo $category[0]->slug;
    ?>-list">
    <a href="<?php the_permalink() ?>" class="block-link">
    		<div class="panel-body show-list">
      <img class="show-logo img-responsive" src="<?php echo $wpbtn_options['logos_dir'] . $btn_feature['logo-small']; ?>" alt="<?php echo $btn_feature['name']; ?>" />
              <?php  if (!is_page()){ ?>
              <span class="label label-<?php echo $labelStyle; ?> pull-right" title="<?php echo $postDate; ?>"><?php echo $releasedText; ?></span><strong><?php echo substr(get_the_title(), 0,49); ?><?php // the_ID(); ?></strong></br>
              <?php  } ?>
                <p class=""><?php echo substr(get_the_excerpt(), 0,115); ?>&hellip;</br>
                <?php if ($btn_feature['name'] == 'Review' || $btn_feature['name'] == 'The Blog') { ?>
                	<span class="label label-success label-as-badge">Read <span class="glyphicon glyphicon-book"></span></span>
                <?php } else { ?>
                	<span class="label label-success label-as-badge">Play <span class="glyphicon glyphicon-play"></span></span>
                <?php } ?>
                </p>
          		</div>
          		</a>
    	</div>
    </div>
    <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    This comes close but the only thing missing is i cannot get the post to appear in posts order. If i move the orderby and order to the $cat_args=array section this simply organises by the date the category was created. Not the posts inside.

    I expect what i need to do is pull in the post array and organise by post_date but i just can’t work out what i need to do.

    Any ideas?

Viewing 1 replies (of 1 total)
  • I’d do it this way:

    -pull down IDs and filtered categories of all posts in descending order by date, put them in array A
    -prepare an array B with keys of filtered categories and null values
    -cycle down the A array. Let’s say ID=190 has cat=15. Look the value of key=15 of B array. If empty fill with ID=190. If not empty just skip.
    -Once B array is full, use the values to pull down the posts by ID in descending order by date and loop them.

    Hope it helps

Viewing 1 replies (of 1 total)
  • The topic ‘Show only the most recent post from each category ordered by post date’ is closed to new replies.