[wp-hackers] Custom post types and wp_get_archives

Aero Maxx aero.maxx.d at gmail.com
Tue Jul 3 20:10:53 UTC 2012


Hi Mike,

I have been trying to work out how to use the code that you wrote, as I 
like that it makes the recent posts widget work, but it seems to mess 
with my blog posts only page in that it shows posts from my custom post 
type aswell as posts from the blog, have tried using !is_page ( array ( 
'Blog', 'blog' ) ) but it doesn't seem to change anything.

Do you have any ideas as to what I could do ?

Also do you or anyone know how I could get a list based archive on my 
side bar for my custom post type, but to only show the last 6 months, as 
its fairly long list otherwise.

Have decided to keep the blog posts and the custom post type archives 
seperate.

On 03/07/2012 19:00, Mike Little wrote:
>
> Aero,
>
> 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
> -- 
> Mike Little
> http://zed1.com/
>




More information about the wp-hackers mailing list