Using WordPress ‘getarchives_join’ PHP filter

The getarchives_join WordPress PHP filter allows you to modify the SQL JOIN clause for retrieving archives.

Usage

add_filter('getarchives_join', 'my_custom_getarchives_join', 10, 2);
function my_custom_getarchives_join($sql_join, $parsed_args) {
    // your custom code here
    return $sql_join;
}

Parameters

  • $sql_join (string) – Portion of the SQL query containing the JOIN clause.
  • $parsed_args (array) – An array of default arguments.

More information

See WordPress Developer Resources: getarchives_join

Examples

Change JOIN clause to include a custom table

Modify the SQL JOIN clause to include a custom table called “my_custom_table”.

add_filter('getarchives_join', 'add_custom_table_to_getarchives_join', 10, 2);
function add_custom_table_to_getarchives_join($sql_join, $parsed_args) {
    global $wpdb;
    $sql_join .= " LEFT JOIN {$wpdb->prefix}my_custom_table ON {$wpdb->prefix}my_custom_table.post_id = {$wpdb->prefix}posts.ID";
    return $sql_join;
}

Include only posts with a specific meta key

Modify the SQL JOIN clause to include only posts with a specific meta key “featured”.

add_filter('getarchives_join', 'filter_archives_by_meta_key', 10, 2);
function filter_archives_by_meta_key($sql_join, $parsed_args) {
    global $wpdb;
    $sql_join .= " INNER JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id AND {$wpdb->postmeta}.meta_key = 'featured'";
    return $sql_join;
}

Include only posts from specific categories

Modify the SQL JOIN clause to include only posts from specific categories (e.g., 2 and 5).

add_filter('getarchives_join', 'filter_archives_by_categories', 10, 2);
function filter_archives_by_categories($sql_join, $parsed_args) {
    global $wpdb;
    $sql_join .= " INNER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id";
    $sql_join .= " INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id";
    $sql_join .= " AND {$wpdb->term_taxonomy}.taxonomy = 'category' AND {$wpdb->term_taxonomy}.term_id IN (2, 5)";
    return $sql_join;
}

Exclude posts from specific authors

Modify the SQL JOIN clause to exclude posts from specific authors (e.g., 3 and 7).

add_filter('getarchives_join', 'exclude_archives_by_authors', 10, 2);
function exclude_archives_by_authors($sql_join, $parsed_args) {
    global $wpdb;
    $sql_join .= " LEFT JOIN {$wpdb->users} ON {$wpdb->posts}.post_author = {$wpdb->users}.ID";
    $sql_join .= " AND {$wpdb->users}.ID NOT IN (3, 7)";
    return $sql_join;
}

Include only posts with a specific tag

Modify the SQL JOIN clause to include only posts with a specific tag “featured-tag”.

add_filter('getarchives_join', 'filter_archives_by_tag', 10, 2); 
function filter_archives_by_tag($sql_join, $parsed_args) { 
global $wpdb; $sql_join .= " INNER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id"; 
$sql_join .= " INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_relationships}.term_taxonomy_id = {$wpdb->term_taxonomy}.term_taxonomy_id"; 
$sql_join .= " INNER JOIN {$wpdb->terms} ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id"; 
$sql_join .= " AND {$wpdb->term_taxonomy}.taxonomy = 'post_tag' AND {$wpdb->terms}.slug = 'featured-tag'"; 
return $sql_join; 
}