Using WordPress ‘getarchives_where’ PHP filter

The getarchives_where WordPress PHP filter allows you to modify the SQL WHERE clause when retrieving archives.

Usage

add_filter('getarchives_where', 'your_custom_function', 10, 2);

function your_custom_function($sql_where, $parsed_args) {
    // your custom code here
    return $sql_where;
}

Parameters

  • $sql_where (string) – The portion of the SQL query containing the WHERE clause.
  • $parsed_args (array) – An array of default arguments.

More information

See WordPress Developer Resources: getarchives_where

Examples

Filter archives by a specific category

Filter the archives list to show only posts from a specific category.

add_filter('getarchives_where', 'filter_archives_by_category', 10, 2);

function filter_archives_by_category($sql_where, $parsed_args) {
    global $wpdb;
    // Replace 'your-category-slug' with the desired category slug
    $category_id = get_category_by_slug('your-category-slug')->term_id;
    $sql_where .= " AND $wpdb->term_taxonomy.term_id = $category_id";
    return $sql_where;
}

Filter archives by custom post type

Filter the archives list to show only posts from a custom post type.

add_filter('getarchives_where', 'filter_archives_by_custom_post_type', 10, 2);

function filter_archives_by_custom_post_type($sql_where, $parsed_args) {
    global $wpdb;
    // Replace 'your-custom-post-type' with your desired custom post type
    $sql_where .= " AND $wpdb->posts.post_type = 'your-custom-post-type'";
    return $sql_where;
}

Filter archives by date range

Filter the archives list to show only posts within a specific date range.

add_filter('getarchives_where', 'filter_archives_by_date_range', 10, 2);

function filter_archives_by_date_range($sql_where, $parsed_args) {
    global $wpdb;
    // Replace 'start_date' and 'end_date' with your desired date range
    $sql_where .= " AND $wpdb->posts.post_date BETWEEN 'start_date' AND 'end_date'";
    return $sql_where;
}

Filter archives by post status

Filter the archives list to show only posts with a specific post status.

add_filter('getarchives_where', 'filter_archives_by_post_status', 10, 2);

function filter_archives_by_post_status($sql_where, $parsed_args) {
    global $wpdb;
    // Replace 'your-post-status' with your desired post status
    $sql_where .= " AND $wpdb->posts.post_status = 'your-post-status'";
    return $sql_where;
}

Filter archives by author

Filter the archives list to show only posts by a specific author.

add_filter('getarchives_where', 'filter_archives_by_author', 10, 2);
function filter_archives_by_author($sql_where, $parsed_args) {
    global $wpdb;
    // Replace 'your-author-id' with the desired author ID
    $sql_where .= " AND $wpdb->posts.post_author = your-author-id";
    return $sql_where;
}