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

How to use get_adjacent_post_join filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
June 8, 2023
5 minutes read

When working with WordPress, you often need to customize the way adjacent posts are fetched. The get_adjacent_post_join filter is just what you need! This filter lets you modify the SQL JOIN clause used to fetch the next or previous post. Whether you’re developing a custom plugin or tweaking your theme, this hook is quite handy.

The dynamic portion of the hook name, $adjacent, refers to the type of adjacency, either ‘next’ or ‘previous’. To use the get_adjacent_post_join filter, first, you have to register it using add_filter. We at WePlugins suggest creating a custom WordPress Plugin for using hooks to ensure no disruptions during theme updates.

Live Example 1: Basic Usage

Here’s a basic example of how to use the get_adjacent_post_join filter:

    function weplugins_modify_get_adjacent_post_join_defaults($join, $in_same_term, $excluded_terms, $taxonomy, $post) { 
        // Update the $join variable according to your website requirements and return this variable.
        return $join; 
    }
    // add the filter
    add_filter( "get_adjacent_post_join", "weplugins_modify_get_adjacent_post_join_defaults", 10, 5 );
    

Live Example 2: Removing a Hook

Sometimes, you need to remove a registered hook. Use remove_filter as demonstrated below:

    remove_filter( "get_adjacent_post_join", "weplugins_modify_get_adjacent_post_join_defaults", 10, 5 );
    

Ensure to provide the same callback function name, priority, and number of arguments when removing the hook callback.

Live Example 3: Conditional Modification

You can also modify the $join variable conditionally based on your requirements:

    function weplugins_conditional_get_adjacent_post_join($join, $in_same_term, $excluded_terms, $taxonomy, $post) { 
        if ($taxonomy == 'category') {
            // Modify join clause if taxonomy is category
            $join .= " /* Custom join modification */ ";
        }
        return $join;
    }
    add_filter( "get_adjacent_post_join", "weplugins_conditional_get_adjacent_post_join", 10, 5 );
    

Access Premium WordPress Plugins

If you’re facing any issues or need 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.