I have a custom post type which has been made to show in archives using the code below in my theme's functions.php :

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
    if ( is_archive() && empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
        $post_types = get_post_types();
            $post_type = $post_type;
            $post_type = $post_types;
        return $query;

The problem is that pre_get_posts basically altered the query for recent posts widget, causing it to display all post types in this case. However, since I am using WooCommerce, the shop products are also shown which I would like to avoid.

So, is there any way I can make the recent posts show only standard WordPress posts and a custom post type, while keeping the custom post type visible in archives?


1 Answer 1


Use the widget_posts_args filter

add_filter('widget_posts_args', 'wpsites_modify_recent_posts_widget');
function wpsites_modify_recent_posts_widget($params) {
$params['post_type'] = array('post', '$post_type');
return $params;


  • Thanks for the help! I think is_main_query works better in the sense that it can avoid problems with other widgets / queries in the page by making sure that only the main query is altered to show the custom post type. wordpress.stackexchange.com/questions/170252/…
    – Billy
    Dec 6, 2014 at 13:37
  • You could use in_the_loop() as well. Dec 6, 2014 at 13:40
  • Thanks for mentioning that. I did a search and found another thread about this : wordpress.stackexchange.com/questions/67716/… But I'm not sure what's the difference between in_the_loop and is_main_query? As in, under what cases will one be preferred / work over the other?
    – Billy
    Dec 6, 2014 at 13:49
  • @Billy they are the same. in_the_loop() was used before is_main_query() existed. Your choice which to use. I personally prefer is_main_query() Dec 6, 2014 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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