Including a custom post type in the Archives widget

The Archive widget is using wp_get_archives() to display the archive.

If you want to target all the wp_get_archives() functions, you can use the getarchives_where filter to add your custom post type:

add_filter( 'getarchives_where', 'custom_getarchives_where' );
function custom_getarchives_where( $where ){
    $where = str_replace( "post_type="post"", "post_type IN ( 'post', 'videos' )", $where );
    return $where;
}

If you want to target only the first Archive widget, you can try

add_action( 'widget_archives_args', 'custom_widget_archives_args' );
function custom_widget_archives_args( $args ){
    add_filter( 'getarchives_where', 'custom_getarchives_where' );
    return $args;
}

with

function custom_getarchives_where( $where ){
    remove_filter( 'getarchives_where', 'custom_getarchives_where' );
    $where = str_replace( "post_type="post"", "post_type in ( 'post', 'videos' )", $where );
    return $where;
}

where the filter is removed, to prevent affecting other parts.

Leave a Comment