questions

How to display featured posts outside of the loop on a category page, where posts must match the category

Rate this post

I am trying to build a “Popular Articles” area on all category pages, above the loop that displays posts. I’ve added a “Featured Post” checkbox on each post that allows the admin to mark certain posts as featured, and I’ve been able to display these posts at the top of each category page. BUT it currently displays ALL featured posts on all category pages, and I need the system to filter posts to show ONLY posts that are in the same category as the page on which they are displaying.

Here is what I’m using in my functions file that works well to display featured posts – any help adding in the category filtering is appreciated!

  $args = array(
        'posts_per_page' => 5,
        'meta_key' => 'meta-checkbox',
        'meta_value' => 'yes'
    );
    $featured = new WP_Query($args);
 
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<p class="details">By <a href="<?php the_author_posts() ?>"><?php the_author(); ?></a> / On <?php echo get_the_date('F j, Y'); ?> / In <?php the_category(', '); ?></p><?php if (has_post_thumbnail()) : ?><figure> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a> </figure>
<p ><?php the_excerpt();?></p><?php
endif;
endwhile; else:
endif;
?>```

 

✔️Solution:

If you only need this for category archive pages and not with other or custom taxonomies, all you need is the global variable $category_name. Even though it’s called “name”, it’s actually the category slug, which allows us to use it in the tax_query field which we will append to our query.

First, we make $category_name available and then use it in our query, along with your meta fields:

global $category_name;

$featured_posts = new WP_Query(
    [
        "showposts"  => 5,
        "meta_key"   => "meta-checkbox",
        "meta_value" => "yes",
        "tax_query"  => [
            [
                "taxonomy" => "category",
                "field"    => "slug",
                "terms"    => $category_name,
            ],
        ]
    ]
);

This will give us posts that are

  • inside the category of the current category archive page,
  • marked by an administrator as featured posts via the meta-checkbox meta key.

Now we can use these posts and loop through them. Here’s a very simple loop with little markup:

if ($featured_posts->have_posts()) {
    while ($featured_posts->have_posts()) {
        $featured_posts->the_post();
        ?>
         <h3>
             The post "<?php the_title(); ?>" is in category "<?php the_category(" "); ?>"
         </h3>
        <?php
    }
}
else {
    ?>
    <h3>No featured posts were found 🙁</h3>
    <?php
}

This is an example of how it looks. As you can see, all five posts are in the same category as the archive page category.

qq9

I hope this helped.

Leave a Reply

Your email address will not be published.

Back to top button