[wp-hackers] Custom post types and wp_get_archives

Mike Little wordpress at zed1.com
Tue Jul 3 18:00:49 UTC 2012


You can try this, add it to your custom post type plugin or your theme's
functions.php. It's fairly brute force but should do most of what you want,
including making the recent posts widget work.

add_action( 'pre_get_posts', 'cpt_add_to_posts' );
function cpt_add_to_posts( $query ) {
$post_type = $query->get( 'post_type' );
 if ( empty( $post_type ) ||  'post' == $post_type )
$query->set( 'post_type', array( 'post', 'cpt' ) );

replacing 'cpt' with your own custom post type

Using Lionel's suggestion
add_filter( 'getarchives_where', 'cpt_getarchives_where' );
function cpt_getarchives_where( $where ) {
return str_replace( "WHERE post_type = 'post'", "WHERE post_type IN
('post', 'cpt')", $where );

will make the archives widget work too.

Mike Little

More information about the wp-hackers mailing list