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();
        if($post_type)
            $post_type = $post_type;
        else
            $post_type = $post_types;
        $query->set('post_type',$post_type);
        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;
} 

Source

Leave a Reply

Your email address will not be published. Required fields are marked *