I am creating a wordpress site that displays posts as images in a grid. I have displayed the categories and tags of the posts before the grid and I'm looking to achieve filtration of the thumnails based on the selected tag or category. However, it doesn't seem to work at the moment. All the posts are displayed even though a tag or category is selected.
<div id="grid">
<div class="container">
<div class="row text-center">
<?php
$thumbnails = get_posts( 'numberposts=500' );
foreach ( $thumbnails as $thumbnail ) {
if ( has_post_thumbnail( $thumbnail->ID ) ) {
echo '<div class="col-md-4 col-lg-4"><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
echo get_the_post_thumbnail( $thumbnail->ID, 'large' );
echo '</a></div>';
}
}
?>
</div>
</div>
</div>
I suspect it has to do with the 'numberposts' but even when I change it, the posts are still not filtered.
Do you know what's causing this?
If you want a filtered list, then you'll need to filter the query results.
If you change your code to:
$thumbnails = get_posts( array(
'numberposts' => 500,
...
) );
then you can add filtering parameters based on your settings that you do not show, e.g.
'tag' => $tag_slug or 'tag_id' => $tag_id
'cat' => $cat_id or 'category_name' => $catSlug