• Resolved vr8ce

    (@vr8ce)


    Is it possible to exclude post formats (not types) from the stock “Recent Posts” widget?

    I’ve searched for this, and found a handful of posts on SO and elsewhere (but none here; I searched for ‘”recent posts” widget’ and looked at the first three pages), but they’re all either hand-coding a widget, or excluding based on something else (category, post type, etc.), or have some other difference.

    I want to exclude my aside posts from the widget. I don’t want to use a plugin. I would prefer to use the stock widget. Is this possible?

    I looked into widget_posts_args, but I’m afraid I don’t understand the documentation. It says the the second parameter is an array of $args, but doesn’t say anywhere what that array can contain. Even the source doesn’t give me any hints, but that’s probably my failing.

    The widget_posts_args docs said see also WP_Query::get_posts(), but that source code didn’t do me much more good. It doesn’t seem to reference post formats anywhere, though, so that might be a clue that this isn’t possible. I just want to make sure before I give up.

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It seems possible, probably through a pre_get_posts filter. See https://codex.wordpress.org/Class_Reference/WP_Query and scroll down to “Multiple Taxonomy Handling”

    You’d have to test if the query were being used by the widget, then exclude specific formats.

    Thread Starter vr8ce

    (@vr8ce)

    Thanks, Steve. I finally had some time to play with this today. Here’s what I did for anyone searching for a similar solution.

    function filter_recent_posts($args) {
        $args = array(
            'post_type' => 'post',
            'tax_query' => array(
                array(
                    'taxonomy' => 'post_format',
                    'field'    => 'slug',
                    'terms'    => array( 'post-format-aside' ),
                    'operator' => 'NOT IN',
                ),
            ),
        );
        return $args;
    }
    add_filter('widget_posts_args', 'filter_recent_posts');
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exclude post format(s) from standard Recent Posts widget’ is closed to new replies.