Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_link_category filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
December 30, 2022
5 minutes read

“`html

post_link_category filter

Filters the category that gets used in the %category% permalink token.

    apply_filters( 'post_link_category', WP_Term $cat, array $cats, WP_Post $post )
    

Description

This hook is used for filtering the category that’s used in the %category% permanent token. This is also used by get_permalink() action which retrieves the full permalink for the current post or post ID.

Parameters

  • $cat : (WP_Term) The category to use in the permalink.
  • $cats : (array) Array of all categories (WP_Term objects) associated with the post.
  • $post : (WP_Post) The post in question.

Live Example

To run the hook, copy the example below.

Example 1: Basic Hook Usage

This example demonstrates the basic use of the post_link_category filter.

    $cats_0 = apply_filters( 'post_link_category', $cats_0, $cats, $post ); 
                             
    if ( !empty( $cats_0 ) ) { 
                             
       // everything has led up to this point... 
                             
    } 
    

Example 2: Adding a Hook Callback

The following example adds a callback to the post_link_category filter.

    // define the post_link_category callback 
    function weplugins_filter_post_link_category( $cats_0, $cats, $post ) { 
        // make filter magic happen here... 
        return $cats_0; 
    }; 
             
    // add the filter 
    add_filter( 'post_link_category', 'weplugins_filter_post_link_category', 10, 3 ); 
    

Example 3: Removing a Hook Callback

To remove a callback from the post_link_category filter, use the example below.

    // remove the filter 
    remove_filter( 'post_link_category', 'weplugins_filter_post_link_category', 10, 3 ); 
    

Access Premium WordPress Plugins

Contact Us

If you need any customization, feel free to contact us.

“`

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.