Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author J B

    (@johnpbloch)

    I did this once and you basically have to rewrite the wp_get_archives function to suit your needs.

    @beetrootman:

    This code should work for you. Use before calling wp_get_archives on the template page. This will generate your archive links with the replacement of post type ‘post’ with your custom post type.

    add_filter( 'getarchives_where' , 'fm_getarchives_where_filter' , 10 , 2 ); 
    
    function fm_getarchives_where_filter( $where , $r ) {
    	$args = array( 'public' => true , '_builtin' => false );
    	$output = 'names'; $operator = 'and';
    	$post_types = get_post_types( $args , $output , $operator );
    	$post_types = array_merge( $post_types , array( 'post' ) );
    	$post_types = "'" . implode( "' , '" , $post_types ) . "'";
    	return str_replace( "post_type = 'post'" , "post_type = 'your_custom_post_type_name'" , $where );
    }

    Just replace your_custom_post_type_name above with your custom post type name and you are all set.

    Regards,
    Fahd Murtaza

    I wont recommend putting it in functions.php as functions.php is for all the wordpress theme files while this filter is to be used on specific template. For a generic archive, if you want to add the custom post types to the results produced by wp_get_archives , then you might have to consider using this code in your functions.php

    add_filter( 'getarchives_where' , 'ucc_getarchives_where_filter' , 10 , 2 ); 
    
    function ucc_getarchives_where_filter( $where , $r ) {
    	$args = array( 'public' => true , '_builtin' => false );
    	$output = 'names'; $operator = 'and';
    	$post_types = get_post_types( $args , $output , $operator );
    	$post_types = array_merge( $post_types , array( 'post' ) );
    	$post_types = "'" . implode( "' , '" , $post_types ) . "'";
    	return str_replace( "post_type = 'post'" , "post_type IN ( $post_types )" , $where ); }

    Source:
    https://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Plugin: Custom Post Permalinks] Yearly archives’ is closed to new replies.