79794136

Date: 2025-10-19 07:18:52
Score: 2
Natty:
Report link

Update:

  1. I tried the code on a new clean environment with clean wordpresss 6.8.3 installed and the result was exactly the same.
  2. Tried on 3 different themes: twenty twenty-four, twenty twenty-five, and astra, all gave the same result.
  3. Tried using ACF + ACF views (the latter generated shortcode to put anywhere you'd like) and the result was still the same.

All above displayed only 'bread' instead of actual category ('bread', 'cake', or 'brownie') where each item belonged. So I assumed this should be something internal of wordpress about shortcode displaying in query loop.

Anyway, I tried another way not using shortcode and this time it works:

function filter_archive_categories( $terms, $post_id, $taxonomy ) {
    if ( $taxonomy !== 'category' || ( ! is_archive() && ! is_home() && ! is_front_page() && ! is_search() ) ) {
        return $terms;
    }
    $allowed_category_slugs = array( 'bread', 'cake', 'brownie' );
    $filtered_terms = array();
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
        foreach ( $terms as $term ) {
            if ( in_array( $term->slug, $allowed_category_slugs, true ) ) {
                $filtered_terms[] = $term;
            }
        }
    }
    return $filtered_terms;
}
add_filter( 'get_the_terms', 'filter_archive_categories', 10, 3 );

This time the result displays correctly. Thanks everyone so far. Hope this helps for those who may have the same problem.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Whitelisted phrase (-1): Hope this helps
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): have the same problem
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: FUFY