Forum

Thread tagged as: Question

Blog Archive by Categories Not Returning Anything

Up until recently I'd been using categories in the admin area but never using them on the front end of my site. I'm changing things around a bit now and want to use categories for people to filter my posts.

I've updated the blog and following a few issues I can now see the checkboxes in the admin area and the posts seem to be assigned but I cannot get the archive.php to return anything.

This is the code I have at present…

<?php           
                // defaults for all modes
                $posts_per_page = 10;
                $template       = 'post_in_list.html';
                $sort_order     = 'DESC';
                $sort_by        = 'postDateTime';

                // Have we displayed any posts yet?
                $posts_displayed = false;

                /* 
                    perch_get() is used to get options from the URL.

                    e.g. for the URL 
                        /blog/archive.php?cat=news

                    perch_get('cat') would return 'news' because cat=news.


                    The code below looks for different options in the URL, and then displays different types of listings based on it.
                */


                /* --------------------------- POSTS BY CATEGORY --------------------------- */
                if (perch_get('cat')) {
                    echo '<h1>Archive of: '.perch_blog_category(perch_get('cat'), true).'</h1>';

                    perch_blog_custom(array(
                            'category'   => perch_get('cat'),
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                    $posts_displayed = true;
                }


                /* --------------------------- DEFAULT: ALL POSTS --------------------------- */

                if ($posts_displayed == false) {

                    // No other options have been used; no posts have been displayed yet.
                    // So display all posts.

                    echo '<h1>Archive</h1>';

                    perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            ));

                }

            ?>

Which is taken from the example files. Visiting the URL /archive.php?cat=design returns nothing. If I drop out the first if statement all the content shows but obviously it's not filtered.

Any pointers? I'm not sure what step to take next.

Thanks

David Ellis

David Ellis 1 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you turn on debug, what does it output?

Nothing on the archive page at all

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you see the closing </html> tag?

Yeah the closing </html> tag is there

Drew McLellan

Drew McLellan 2638 points
Perch Support

And you're outputting the debug below that point?

I wasn't actually, am now - here's the debug…

Debug Message
Fetching from cache: perch_blog_category31c13f47ad87dd7baa2d558a91e0fbb9
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/design/%' OR idx.indexKey='_category' AND idx.indexValue='blog/design/')
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 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-04-13 11:00:00' ) GROUP BY itemID ORDER BY sortval DESC LIMIT 0, 10
SELECT FOUND_ROWS() AS `count`
SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
Using template: /addons/apps/perch_blog/templates/blog/post_in_list.html
SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/blog/archive.php' OR regionPage='*' ORDER BY regionPage DESC
Drew McLellan

Drew McLellan 2638 points
Perch Support

It doesn't look like there are posts in that category. Are there?

Damn! Yes there are but it looks like they all need re-saving - cheers for the help Drew!