0

I would like to get monthly archives for static pages. I tried to use the next code:

add_filter('getarchives_where','my_archives_filter');
function my_archives_filter($where_clause) {
return "WHERE post_type = 'page' AND post_status = 'publish'";
}

and in the sidebar I write:

add_filter('getarchives_where','my_archives_filter');               
wp_get_archives('type=yearly');
remove_filter ('getarchives_where','my_archives_filter');

But it doesn't work.

Can you help me?

Thanks.

2

wp_get_archives already have post_type = 'post' by default and to replace it try this:

add_filter('getarchives_where','my_archives_filter');
function my_archives_filter($where_clause) {
    str_replace( "post_type = 'post'" , "post_type = 'page'" , $where );
}

in sidebar:

add_filter('getarchives_where','my_archives_filter');               
wp_get_archives('type=yearly');
remove_filter ('getarchives_where','my_archives_filter');
  • Thanks for your answer. Now in sidebar I get yearly archive but it gives me a 404 error when I follow de link (thedomain.com/2012) – Rec Apr 15 '12 at 15:45
  • That is beacuse the archive template is set to show posts and not pages – Bainternet Apr 15 '12 at 16:06
  • And how can I change it? – Rec Apr 15 '12 at 16:23
1

The add_filter followed by remove_filter approach seems hacky, I prefer using a conditional approach like this:

/**
 * Post type archives by month
 */
function kevinlearynet_getarchives_events( $sql_where, $r ) {

    // conditions for when you want to do this
    // (don't do it all the time)
    if ( is_post_type_archive( 'event' ) ) {
        $sql_where = str_replace( "post_type = 'post'" , "post_type = 'event'" , $sql_where );
    }

    return $sql_where;
}
add_filter( 'getarchives_where', 'kevinlearynet_getarchives_events', 10, 2 );
0

If you are just trying to change the post_type to page, you do not need to use the filter. Just do the following:

wp_get_archives( array(
'post_type' => 'page'
) );

The problem with this is that the archives is geared toward posts not pages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.