BuddyDev

Search

[Resolved] BuddyBlog Pro – exclude from recent posts

  • Participant
    Level: Master
    Posts: 279
    NikB on #47467

    Hi Brajesh

    I currently have things set up so that personal posts appear in the main blog, whereas group posts only appear in the group in which they are posted.

    This all works fine except that widgets/plugins showing recent or related posts don’t take this into account which means that, for example, group posts are now appearing in the recent posts section on the home page, in side bars etc.

    Can you suggest what would be the best approach to prevent this from happening?

    With many thanks in advance.
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47468

    Hi Nik,
    Thank you for the question.

    For group posts we are adding a meta key ‘_bblgroups_group_id’.

    You can use it to exclude posts by filtering on pre_get_posts or any other way.

    If it is the core WordPress recent posts widget, please let me know. I can look into it and add the support for exuding there.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 279
    NikB on #47471

    Hi Brajesh

    Thank you for getting back to me. Yes I’m using the core WordPress recent posts widget on some of the pages so if you can let me know the code for that, it would be very helpful. (I’m also using Elementor but that allows you to use a custom query so if you can possibly give me the example for the core widget, I can probably figure this part out myself.)

    With many thanks in advance.
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47476

    Hi Nik,
    WordPress provides us with a filter to customize the args Recent posts widget passes to WP_Query.

    We can use code like below

    
    
    add_filter( 'widget_posts_args', function ( $args ) {
    
    	$args['meta_key']     = '_bblgroups_group_id';
    	$args['meta_compare'] = 'NOT EXISTS';
    
    	//or
    	/*
    	$args['meta_query'] = array(
    		array(
    			'key'     => '_bblgroups_group_id',
    			'compare' => 'NOT EXISTS'
    		)
    	);*/
    
    	return $args;
    } );
    
    

    to exclude it.

    You can either use the meta_key, meta_compare approach or the meta_query approach if you are doing multiple meta query. only one of these conditions are required.

    Please let me know if it works or not?

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 279
    NikB on #47480

    Hi Brajesh

    Thank you so much. I’m pleased to report that both of the options you’ve suggested seem to work perfectly.

    In waiting for your reply, I’d also put together my own custom query for the Elementor posts widget along very similar lines and which I thought I would post here for reference and/or just in case it’s of interest to anyone else…

    add_action( 'elementor/query/my_custom', function( $query ) {
    // Get current meta Query
    	$meta_query = $query->get( 'meta_query' );
    
    	// If there is no meta query when this filter runs, it should be initialized as an empty array.
    	if ( ! $meta_query ) {
    		$meta_query = [];
    	}
    
    	// Append our meta query
    	$meta_query[] = [
    		'key' => '_bblgroups_group_id',
    		'compare' => 'NOT EXISTS',
    	];
    
    	$query->set( 'meta_query', $meta_query );    
    } );

    Note: I did wonder if I’d need to do the same for “Related Posts” widgets but so far I can’t find any evidence of group posts appearing as related to personal posts. Not sure if this is by luck or design(!) but I’ll monitor just in case.

    As always, many thanks for your assistance.
    Regards
    Nik

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #47484

    Hi Nik,
    That’s awesome! you had already done it 🙂

    I am not sure about elementor and if that will apply to general WordPress query as haven’t used it recently.

    Please let me know if it works for all.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 279
    NikB on #47490

    Hi Brajesh

    I think/hope it should work anywhere that Elementor Pro allows you to use a custom query, but I’ll get back to you if I come across anywhere that it doesn’t 😉

    Regards
    Nik

The topic ‘ [Resolved] BuddyBlog Pro – exclude from recent posts’ is closed to new replies.

This topic is: resolved