• Hi,

    How to display only the posts of the current category at Recent posts widget?

    I mean by that, for example, while I’m reading an post that categorized as “wordpress”, I want to see more post of same category at recent posts widget.

    It is for the purpose of relevancy and user experience and also a little SEO consideration.

    Appreciate your time,
    Michael

    • This topic was modified 2 years, 6 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Use the ‘widget_posts_args’ filter. Callback code could get the current post’s category (for singular posts), then add something like
    'cat' => $category_id,
    to the passed args array.

    This worked for the legacy widget. I’m unsure if it still does for block based widgets. I think it does, but I’ve not verified.

    Thread Starter michaelcoco

    (@michaelcoco)

    @bcworkz

    Do you have the full code for that?

    It seems the right solution for my “wish-list” ??

    Moderator bcworkz

    (@bcworkz)

    Something like this:

    add_filter('widget_posts_args', 'my_restrict_cat', 10, 2 );
    function my_restrict_cat( $args, $instance ) {
      if ( is_single()) {
        $cats = wp_get_post_categories( get_queried_object_id(), ['fields' => 'ids',]);
        $args['cat'] = $cats[0];
      }
      return $args;
    }

    Untested though. Only restricts widget queries on single post pages. Uses the first category term in the returned list regardless of how many are assigned. You can add this to your theme’s functions.php, but if the theme is subject to periodic updates it should be in a simple custom plugin instead. Plugins aren’t all that difficult to create.

    $args is passed directly to a new WP_Query object, so you can alter the query in any other way you like as well.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Recent posts widget : how to display only the posts of the current category?’ is closed to new replies.