WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Permalinks
Yearly archives (4 posts)

  1. beetrootman
    Member
    Posted 3 years ago #

    Ok, this plugin works great and sets up my permalinks so I can access yearly archives like so:

    http://mysite/custom-type/2010

    But do you have any idea how I can actually generate a list in a template that will link to the yearly archives. wp_get_archives has no option for custom post types.

    http://wordpress.org/extend/plugins/custom-post-permalinks/

  2. John P. Bloch
    Member
    Plugin Author

    Posted 3 years ago #

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

  3. Fahd Murtaza
    Member
    Posted 2 years ago #

    @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

  4. Fahd Murtaza
    Member
    Posted 2 years ago #

    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:
    http://bajada.net/2010/07/15/adding-custom-post-types-to-wp_get_archives

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic