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

How to use getarchives_where filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 17, 2023
5 minutes read

Ever wondered how to filter the SQL WHERE clause when retrieving archives in WordPress? The getarchives_where filter is your go-to hook for this task. Let’s dive into how you can use this hook to customize your WordPress site.

Before you can use the getarchives_where filter, you need to register it using add_filter. You can add this code to your theme’s functions.php file or, even better, in a custom WordPress plugin to ensure your customizations remain intact during theme updates.

In this guide, we’ll look at three live examples, each demonstrating a different use case for the getarchives_where filter. Let’s get started!

Live Example #1: Filtering Archives by Category

This example shows how to filter archives by a specific category.

	add_filter('getarchives_where', 'weplugins_custom_archive_by_category_where');
	add_filter('getarchives_join', 'weplugins_custom_archive_by_category_join'); // You must add `join` to keep it works. Adding only `where` will return nothing
	
	function weplugins_custom_archive_by_category_where($x) {
	    global $wpdb;
	    $current_term_slug = get_query_var('news_category');
	    if (!empty($current_term_slug)) {
	        $current_term = get_term_by('slug', $current_term_slug, 'news_category');
	        // Your custom logic here
	    }
	    return $x;
	}
	

Live Example #2: Modifying Default Arguments

Here’s how you can modify the default arguments using the getarchives_where filter.

	function weplugins_modify_getarchives_where_defaults($sql_where, $parsed_args) {
	    // Update the $sql_where variable according to your website requirements and return this variable.
	    return $sql_where;
	}
	
	// Add the filter
	add_filter('getarchives_where', 'weplugins_modify_getarchives_where_defaults', 10, 2);
	

Live Example #3: Removing a Hook Callback

If you need to remove a previously registered hook, you can use the remove_filter function as shown below.

	remove_filter('getarchives_where', 'weplugins_modify_getarchives_where_defaults', 10, 2);
	

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

Below are the 2 parameters required to use this hook:

  • $sql_where: (string) Portion of SQL query containing the WHERE clause.
  • $parsed_args: (array) An array of default arguments.

If you’re having any trouble using this hook, please don’t hesitate to contact our WordPress Development Team and we’d be happy to assist you.

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.