Forum

Thread tagged as: Question, Addons, Blog

Sort Blog posts by Post Type

Is there an ID or something that would allow me to sort blog posts by type? Currently I'm having to make the user select a post type at the beginning of creating a post and then also go to the Meta tab and put it in the appropriate Section. It would be awesome if the post type selection was all that was needed.

Chris Gerringer

Chris Gerringer 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm confused as to how sorting by type relates to sections - could you give an example?

Sorry, I meant that since I couldn't figure out how how to simply sort by type I'm having the user select a post type, which is the template for the post, but then also go and select a section to put it in, which is how I'm actually sorting them for display on the site.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can sort on postTemplate, which is the post type.

Thanks Drew, got that figured out, but I'm having trouble filtering posts for previous/next links within a post. I can't seem to figure out why this code isn't working:

<?php

  $slug = perch_get('s');

  $html = perch_blog_post($slug, true);

  $data = perch_blog_custom(array(
    'filter'=>'postSlug',
    'match'=>'eq',
    'value'=>$slug,
    'skip-template'=>true, 
  ));

  $data = $data[0];
  $date = $data['postDateTime'];

  $prev = perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'filter'=>array(
      array(
        'filter'=>'postDateTime',
        'match'=>'lt',
        'value'=>$date,
      ),
      array(
        'filter' => 'postTemplate',
        'match' => 'eq',
        'value' => 'posts/comic.html',
      ),
    ),
    'count'=>1,
    'template'=>'post_prev.html'
  ), true);

  $next = perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'ASC',
    'filter'=>array(
      array(
        'filter'=>'postDateTime',
        'match'=>'gt',
        'value'=>$date,
      ),
      array(
        'filter' => 'postTemplate',
        'match' => 'eq',
        'value' => 'posts/comic.html',
      ),
    ),
    'count'=>1,
    'template'=>'post_next.html'
  ), true);

  ?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

How is it failing?

It seems not to output anything, but I think I found a different solution anyway. Realized the next/previous links really need to be within categories anyway, so I'm filtering by category now and that seems to work fine. Thanks!