apply_filters( 'get_the_categories', WP_Term[] $categories, int|false $id )

Filters the array of categories to return for a post.


Parameters Parameters

$categories

(WP_Term[]) An array of categories to return for the post.

$id

(int|false) ID of the post.


Top ↑

Source Source

File: wp-includes/category-template.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Added $id parameter.
3.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Sumit Singh

    Remove certain categories from being display on post loop for a specific post.

    Assume we want to remove categories with slug cat-slug-a and cat-slug-b for post with ID 5

    /**
     * Remove certain categories on post loop for a specific post
     * @param array $categories Array of categories
     * @return array $categories filtred categories
     */
    function wpdocs_remove_selected_categories( $categories ) {
    	if ( 5 == get_the_ID() ) { // Check if it is a specific post.
    		
    		$categories_to_remove = array(
    			'cat-slug-a',
    			'cat-slug-b'
    		); // Array of categories slug to be remove.
    
    		foreach ( $categories as $index => $single_cat ) {
    
    			if ( in_array( $single_cat->slug, $categories_to_remove ) ) {
    				unset( $categories[ $index ] ); // Remove the category.
    			}
    		}
    	}
    	
    	return $categories;
    }
    add_filter( 'get_the_categories', 'wpdocs_remove_selected_categories' );
    

You must log in before being able to contribute a note or feedback.