Joshc
Joshc

Reputation: 3853

wp_get_archives for custom post type not working?

I'm using wp_get_archives to retrieve a monthly list of dates for posts have been posted in my custom post type 'download'.

But it is not working, what I am doing wrong?

I am using workpress 3.3

Thanks

CODE

<?php   $args       = array(

    'post_type'     => 'download',
    'type'          => 'monthly',
    'show_count'    => '1'

); ?>

<?php wp_get_archives( $args ); ?>


THIS IS HOW I REGISTERED MY CUSTOM POST TYPE FOR REFERENCE

// Downloads Post Type
add_action( 'init', 'create_post_type' );
function create_post_type() {
    $args = array(
        'labels' => post_type_labels( 'Download' ),
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'has_archive' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array( 'group' ),
        'supports' => array('title',
            'editor',
            'author',
            'thumbnail',
            'excerpt',
            'comments'
        )
    ); 

    register_post_type( 'download', $args );

}

Upvotes: 1

Views: 9217

Answers (2)

Andrei Bexa
Andrei Bexa

Reputation: 86

Some hints:

Hint 1 Use custom-post-type-archives plugin (update Permalinks after any change)

Hint 2 WPML plugin + custom-post-type-archives:

function CPT1_join( $join ) {
    global $wpdb;
    $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE1' ";
    return $join ;
}

function CPT2_join( $join ) {
    global $wpdb;
    return $join = " JOIN pav_icl_translations t ON t.element_id = pav_posts.ID AND t.element_type='CUSTOM_POST_TYPE2' "; 
}

function wp_get_archives_filter($where, $options) {
    global $wpdb; // get the DB engine

    if(!isset($options['post_type'])) return $where; // OK - this is regular wp_get_archives call - don't do anything

    $post_type = $wpdb->escape($options['post_type']); // escape the passed value to be SQL safe

   if($post_type == 'all')
           $post_type = ''; // if we want to have archives for all post types
    else $post_type = "post_type = '$post_type' AND"; // otherwise just for specific one

    if($options['post_type'] == 'CUSTOM_POST_TYPE1'){
        add_filter( 'getarchives_join', 'CPT1_join' );      
        }elseif($options['post_type'] == 'CUSTOM_POST_TYPE2'){ 
        add_filter( 'getarchives_join', 'CPT2_join' );
        }

   $where = str_replace('post_type = \'post\' AND', $post_type, $where);
   return $where;
}
add_filter('getarchives_where', 'wp_get_archives_filter', 10, 2);

Upvotes: 0

noponies
noponies

Reputation: 2726

wp_get_archives( ); does not support 'post_type' as an argument.

There is a discussion [here][1]: http://wordpress.org/support/topic/archive-list-and-page-for-custom-post-types-mysql?replies=9 about hacking in support for it. The article has a few links off to another solution using a filter which is applied to the sql query for selecting posts to include in the archives result. Looking at how the filter is applied in general-template.php it should also work.

Upvotes: 1

Related Questions