2

I have a custom post type called "videos" that I want to include in the Archives widget (stock widget in the TwentyTwelve theme). It DOES already appear on the archives page, but just not in the widget.

I already have

add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'videos' ) );
return $query;
}

in functions.php - can I modify the IF statement to be something like "if main query OR archive widget query"? How can I do this?

2
  • what is the status of this question, is it still unsolved?
    – birgire
    May 24, 2013 at 0:26
  • 1
    Unfortunately still unsolved, see my reply to your answer below. However, I have decided to use a plugin for this, so I don't really need a solution anymore.
    – Boris4ka
    May 24, 2013 at 4:30

2 Answers 2

4

The Archive widget is using wp_get_archives() to display the archive.

If you want to target all the wp_get_archives() functions, you can use the getarchives_where filter to add your custom post type:

add_filter( 'getarchives_where', 'custom_getarchives_where' );
function custom_getarchives_where( $where ){
    $where = str_replace( "post_type = 'post'", "post_type IN ( 'post', 'videos' )", $where );
    return $where;
}

If you want to target only the first Archive widget, you can try

add_action( 'widget_archives_args', 'custom_widget_archives_args' );
function custom_widget_archives_args( $args ){
    add_filter( 'getarchives_where', 'custom_getarchives_where' );
    return $args;
}

with

function custom_getarchives_where( $where ){
    remove_filter( 'getarchives_where', 'custom_getarchives_where' );
    $where = str_replace( "post_type = 'post'", "post_type in ( 'post', 'videos' )", $where );
    return $where;
}

where the filter is removed, to prevent affecting other parts.

7
  • This doesn't seem to work. The first issue is that it shows randomly inflated post counts for each month - for example, one month went from 5 to 29. The second issue is that the links don't include the ?post_type=videos part of the URL, so they don't work. However, I have found a plugin called "Custom Post Type Archive" that does provide a widget that works. So I have decided to use that instead of coding it into the theme. Thanks for the help!
    – Boris4ka
    May 24, 2013 at 4:27
  • ok good to hear you found a solution. One would have to use the pre_get_posts filter to add extra post types to the archive templates display, but appart from that I only tested it on the default theme TwentyTwelve where it worked ok in my case.
    – birgire
    May 24, 2013 at 7:58
  • @birgire your code have a problem, that is why the post count is way off. return $clauses; should be return $where;. I've made the appropriate edit to your code :-) Jul 16, 2014 at 14:02
  • doh! thanks @PieterGoosen for correcting these silly typos ;-)
    – birgire
    Jul 23, 2014 at 10:56
  • Pleasure. Answered a really big question about archive page and archive widgets, used your code for the widget (with credit), and the user came back with the same issue with the post count. :-), so fixed my answer and yours. Lol Jul 23, 2014 at 11:02
-1

It worked for me:

class CPT_Archives extends WP_Widget
{
    function widget()
    {
        ...

        add_filter('getarchives_where', array($this, 'getarchives_where_filter'), 10, 2);

        $args = array(
            'format' => 'option',
            'echo'   => false,
        );
        $options = wp_get_archives($args);
        $options = str_replace('?m=', '?post_type=CPT&m=', $options);

        echo '<select name="CPT_archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">';
        echo '  <option value="">' . esc_attr( __( 'Select Month' ) ) . '</option>';
        echo $options;
        echo '</select>';

        ...

    }

    function getarchives_where_filter($where)
    {
        $where = str_replace( "post_type = 'post'", "post_type = 'CPT'", $where );
        return $where;
    }
}
1
  • 1
    Please explain what you've done rather than simply posting code.
    – s_ha_dum
    Mar 13, 2014 at 22:42

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.