Forum

Thread tagged as: Question

duplicating results

I have a small section on the home page which reveals the first 3 posts in each category : https://gochattervideos.com however to do I original used this code :

<?php
                // defaults for all modes
                $posts_per_page =  3;
                $template       = 'home_post_in_list.html';
                $sort_order     = 'DESC';
                $sort_by        = 'postDateTime';
                $seasonal       = 'seasonal';
                $news           = 'news-events';
                $topic          = 'topics';
                $real           = 'real-lives';
                $christmas      = 'christmas';


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

                if ($posts_displayed == false) {

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

                perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'category'   => $seasonal,
                            ));
                }
                perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'category'   => $news,
                            ));
                }
                perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'category'   => $topic,
                            ));
                }
                perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'category'   => $real,
                            ));
                }
                perch_blog_custom(array(
                            'template'   => $template,
                            'count'      => $posts_per_page,
                            'sort'       => $sort_by,
                            'sort-order' => $sort_order,
                            'category'   => $christmas,
                            ));
                }


                ?>

The problem with this is that if a post has more than one category then it is repeated on the page and displays incorrectly. How do I call 3 post from each category but without duplicating the results. for the time being I have put this fixed in place

<?php
                // defaults for all modes
                $posts_per_page =  15;
                $template       = 'home_post_in_list.html';
                $sort_order     = 'DESC';
                $sort_by        = 'postDateTime';
                $seasonal       = 'seasonal';
                $news           = 'news-events';
                $topic          = 'topics';
                $real           = 'real-lives';
                $christmas      = 'christmas';


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

                if ($posts_displayed == false) {

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

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


                ?>

I have called the amount I need a hoped that it pulls three from each category and used an overflow:hidden on the parent div to hide more than 4 results but this is a terrible fix.

Timothy Rackham

Timothy Rackham 0 points

  • 5 years ago
Rachel Andrew

Rachel Andrew 394 points
Perch Support

You can pass in an array of categories, see the docs here: https://docs.grabaperch.com/functions/blog/perch-blog-custom/

category: A single category slug or array of category slugs to return content for. To exclude a category, prefix its name with an exclamation point.

please could you show me ?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

All of the *_custom functions work in the same way as perch_content_custom. There are examples in the documentation: https://docs.grabaperch.com/functions/content/perch-content-custom/

We've written about the small amount of PHP you need ot know to work with custom functions here: https://docs.grabaperch.com/perch/building/servers/how-do-i-get-started-with-php/

I do try reading all this but Im unsure of how to fix this issue, please could you show me how to fix it?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you trying to get the first three posts from a selection of categories, or the first post from each of a selection of categories?

I am trying to get three post from each category to echo but don't want any post to repeat, so I have 5 different categories therefore I need 15 unique posts but three post for each category. if you view it on the web page you should understand https://gochattervideos.com

Any ideas on a fix ? I can't get it working.

Duncan Revell

Duncan Revell 78 points
Registered Developer

I'm not sure there's a "fix" as such - you're probably asking for more than the built-in filtering can provide. You're probably going to have to get your hands dirty with some PHP!

  • I'm guessing a little bit, but you will probably have to get a list of ALL the posts that are in the 5 categories and make an array of their IDs (but don't output anything to the page).
  • Then get your three posts from the first category - but also note the post IDs and remove those IDs from the big array above.
  • Then get your three posts from the second category, but include a filter that matches IDs "IN" the big array (but you will have to turn the array values into a comma-separated string). Note the post IDs and remove those IDs from the array.
  • Do the above line again with the third category.
  • Etc.
  • Etc.

There's a couple of ways to get the IDs and remove from the array, but they involve some PHP work.

I haven't tried this myself, but I think the theory is sound...

Duncan Revell

Duncan Revell 78 points
Registered Developer

Actually, my previous answer is a bit rubbish. You can probably do it with one query utilising "each" and a closure to update a counter for each category. It adds a bit more complexity but might be a neater solution than multiple queries. Either way, coding is needed.