Forum

Thread tagged as: Question, Blog

Filtering Blog posts by category

Hi,

I need to display all blog posts except those with the category 'logistics'.

I have the following code for the listing page:

<?php
 perch_blog_custom(array(
  'sort' => 'postDateTime',
  'sort-order'=>'DESC',
  'count'=>'5',
  'category' => array('!blog/logistics'),
  'template'=>'blog/post_in_list.html',
 ));
?>

If a post is given the category 'logistics' it is excluded from the listing as expected. However, if no posts are assigned the category 'logistics' nothing displays in the listing at all.

Is there a way to make all posts display when no category has be assigned? Like checking for an empty category perhaps?

Andy Knight

Andy Knight 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

However, if no posts are assigned the category 'logistics' nothing displays in the listing at all.

Can you show me the debug output when this happens?

Sure...

Debug Message
SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' WHERE 1=1 AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-11-24 11:43:00' ) GROUP BY itemID ORDER BY sortval DESC LIMIT 0, 5
SELECT FOUND_ROWS() AS `count`
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /templates/blog/post_in_list.html
SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
SELECT idx.itemID FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' AND (idx.indexKey='_category' AND idx.indexValue LIKE 'blog/logistics%' OR idx.indexKey='_category' AND idx.indexValue='blog/logistics')
SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' AND idx.itemID NOT IN (NULL) WHERE 1=1 AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID ) as tbl WHERE (postStatus='Published' AND postDateTime<='2015-11-24 11:43:00' ) GROUP BY itemID ORDER BY sortval DESC LIMIT 0, 5
SELECT FOUND_ROWS() AS `count`
Using template: /templates/blog/post_in_list.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, thanks, I'll see if I can fix it.

Hi Drew,

Just updated the site to 2.8.17 and it seems to have fixed the issue. I saw that you had a note - Fixes bug with negative category filtering - was this the fix?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's the fix!

Brilliant - great work Drew, thanks!