First of all, don’t ever start defining your variables inside the loop (and definitely not an if statement inside it), do it like in the cookbook recipe you mentioned (that’s why we write these things ):
<?php
// First, we define our set of unfiltered articles
$articles = $page->children()->listed()->sortBy('date', 'asc'); // on a side note, I removed flipping as it is superflous, use `asc` or `desc` as required.
// In case of a URL parameter, we filter the `$articles` by the given field
// If you use spaces or other special characters in your tags, `urlencode()` the tag when creating the link and `urldecode()` again here
if ($type = param('type')) {
$articles = $articles->filterBy('typenews', $type, ',');
}
// we apply pagination to the collection as a last step
$articles->paginate(6);
// it usually makes sense to also define the pagination object for the navigation:
$pagination = $articles->pagination();
?>
Then loop through articles
<?php foreach ($articles as $article) : ?>
<!-- your markup here -->
<?php endforeach ?>
Yes, that code should work. Unfortunately, you don’t tells us what exactly does not work.
So I can only make assumptions. Assuming that filtering doesn’t take place as expected, I wonder if you are actually using the $articles variable in your template’s foreach loop?